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 __pPreviousSurface = EGL_NO_SURFACE;
131 EGLSurface __pPreviousContext = EGL_NO_CONTEXT;
134 #if !defined (_OSP_EMUL_)
135 #define FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP
140 ENGINE_TYPE_SOFEWARE_X11 = 0,
141 ENGINE_TYPE_OPENGL_X11,
145 _engineType __evasEngineType = ENGINE_TYPE_SOFEWARE_X11;
146 bool __isDoublePixmapEnabled = false;
148 void _OnBoundsChanged(void* pData);
150 const int INVALID_SGL_INDEX = 0;
151 const int MAX_SGL_INDEX = 100;
157 : sglIndex(INVALID_SGL_INDEX)
158 , display(EGL_NO_DISPLAY)
159 , surface(EGL_NO_SURFACE)
160 , context(EGL_NO_CONTEXT)
161 , config((EGLConfig)0)
164 , nativePixmap((EGLNativePixmapType)0)
165 , pVisualElement(null)
166 , pVisualElementSurface(null)
169 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
178 , pSecondSglInfo(null)
182 virtual ~_SglInfo(void)
184 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
185 if (pEcoreImage != null)
187 ecore_x_image_free(pEcoreImage);
196 if (pVisualElement != null)
198 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
199 if (pVisualElementImpl != null)
201 pVisualElementImpl->SetDestroyedCallback(null, null);
205 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
206 _UnregisterRenderCallback(pObject);
208 evas_object_del(pObject);
209 delete pVisualElementSurface;
212 bool needAllocateObject = true;
213 if (__isDoublePixmapEnabled)
217 needAllocateObject = false;
220 if (needAllocateObject)
222 if (pVisualElement != null)
224 pVisualElement->SetSurface(null);
228 sglIndex = INVALID_SGL_INDEX;
229 display = EGL_NO_DISPLAY;
230 surface = EGL_NO_SURFACE;
231 context = EGL_NO_CONTEXT;
232 config = (EGLConfig)0;
235 nativePixmap = (EGLNativePixmapType)0;
236 pVisualElement = null;
237 pVisualElementSurface = null;
240 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
249 pSecondSglInfo = null;
259 Evas_Object* pObject;
260 EGLNativePixmapType nativePixmap;
261 VisualElement* pVisualElement;
262 VisualElementSurface* pVisualElementSurface;
264 Tizen::Ui::Window* pWindow;
265 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
266 Ecore_X_Image* pEcoreImage;
274 _SglInfo* pSecondSglInfo;
277 _SglInfo(const _SglInfo& sglInfo);
278 _SglInfo& operator =(const _SglInfo& rhs);
285 : nativePixmap((Pixmap)0)
286 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
288 , pTbmBufferObject(null)
294 virtual ~_PixmapInfo(void)
296 Display* pDisplay = (Display*)ecore_x_display_get();
297 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
298 tbm_bo_unmap(pTbmBufferObject);
299 tbm_bo_unref(pTbmBufferObject);
305 tbm_bufmgr_deinit(pTbmBufMgr);
306 DRI2DestroyDrawable(pDisplay, nativePixmap);
308 XFreePixmap(pDisplay, nativePixmap);
310 nativePixmap = (Pixmap)0;
311 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
313 pTbmBufferObject = null;
320 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
321 tbm_bufmgr pTbmBufMgr;
322 tbm_bo pTbmBufferObject;
327 void _EvasObjectImageChange(_SglInfo* pSglInfo);
330 _OnBoundsChanged(void* pData)
332 _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
333 SysTryReturnVoidResult(NID_GRP, pSglInfo != null
334 && pSglInfo->sglIndex > INVALID_SGL_INDEX
335 && pSglInfo->pVisualElement != null
336 && pSglInfo->pWindow != null
337 , E_INVALID_STATE, "Invalid state.");
339 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
341 __pPreviousSglInfo = null;
344 bool needSglMakeCurrent = false;
345 EGLDisplay eglDisplay = eglGetCurrentDisplay();
346 EGLSurface drawSurface = _SglGetCurrentSurface(EGL_DRAW);
347 EGLSurface readSurface = _SglGetCurrentSurface(EGL_READ);
348 EGLSurface eglContext = eglGetCurrentContext();
350 if (__pPreviousSurface == drawSurface)
352 __pPreviousSurface = EGL_NO_SURFACE;
355 if (drawSurface != EGL_NO_SURFACE && readSurface != EGL_NO_SURFACE)
357 needSglMakeCurrent = true;
361 drawSurface = eglGetCurrentSurface(EGL_DRAW);
362 readSurface = eglGetCurrentSurface(EGL_READ);
365 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
366 EGLBoolean ret = eglDestroySurface(pSglInfo->display, pSglInfo->surface);
367 pSglInfo->surface = null;
368 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
370 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
371 if (pSglInfo->pEcoreImage != null)
373 ecore_x_image_free(pSglInfo->pEcoreImage);
374 pSglInfo->pEcoreImage = null;
378 FloatRectangle rect = pSglInfo->pVisualElement->GetBounds();
379 int width = int(rect.width);
380 int height = int(rect.height);
381 SysTryReturnVoidResult(NID_GRP, width > 0 && height > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
382 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
383 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
384 SysTryReturnVoidResult(NID_GRP, widthPhysical > 0 && heightPhysical > 0, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", widthPhysical, heightPhysical);
386 evas_object_image_size_set(pSglInfo->pObject, widthPhysical, heightPhysical);
387 evas_object_resize(pSglInfo->pObject, widthPhysical, heightPhysical);
389 delete pSglInfo->pBitmap;
390 pSglInfo->pBitmap = null;
391 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
392 SysTryReturnVoidResult(NID_GRP, bitmap.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
393 result r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
394 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
395 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfo->sglIndex, bitmap.get());
396 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
397 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfo->display, pSglInfo->config, nativePixmap, null);
398 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
400 pSglInfo->nativePixmap = nativePixmap;
401 pSglInfo->surface = eglSurface;
402 pSglInfo->isBackbuffer = true;
403 pSglInfo->wasShown = false;
404 pSglInfo->swapDone = false;
405 pSglInfo->isFirstSwap = true;
407 if (__isDoublePixmapEnabled)
409 _SglInfo* pSglInfoSecond = pSglInfo->pSecondSglInfo;
410 SysTryReturnVoidResult(NID_GRP, pSglInfoSecond != null, E_INVALID_STATE, "Invalid pSecondSglInfo.");
412 eglMakeCurrent(pSglInfo->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
413 ret = eglDestroySurface(pSglInfoSecond->display, pSglInfoSecond->surface);
414 pSglInfoSecond->surface = null;
415 SysTryReturnVoidResult(NID_GRP, ret == EGL_TRUE, E_OPERATION_FAILED, "eglDestroySurface failed.");
417 evas_object_image_size_set(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
418 evas_object_resize(pSglInfoSecond->pObject, widthPhysical, heightPhysical);
420 delete pSglInfoSecond->pBitmap;
421 pSglInfoSecond->pBitmap = null;
422 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
423 SysTryReturnVoidResult(NID_GRP, bitmapSecond.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
424 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
425 SysTryReturnVoidResult(NID_GRP, r == E_SUCCESS, r, "Propagating.");
426 EGLNativePixmapType nativePixmap = _CreateNativePixmap(pSglInfoSecond->sglIndex, bitmapSecond.get());
427 SysTryReturnVoidResult(NID_GRP, nativePixmap != (EGLNativePixmapType)0, E_OPERATION_FAILED, "Propagating.");
428 EGLSurface eglSurface = eglCreatePixmapSurface(pSglInfoSecond->display, pSglInfoSecond->config, nativePixmap, null);
429 SysTryReturnVoidResult(NID_GRP, eglSurface != EGL_NO_SURFACE, E_OPERATION_FAILED, "eglCreatePixmapSurface failed.");
431 pSglInfoSecond->nativePixmap = nativePixmap;
432 pSglInfoSecond->surface = eglSurface;
433 pSglInfoSecond->pBitmap = bitmapSecond.release();
434 pSglInfoSecond->isBackbuffer = true;
435 pSglInfoSecond->wasShown = false;
436 pSglInfoSecond->swapDone = true;
438 pSglInfo->isFirstSwap = true;
440 _EvasObjectImageChange(pSglInfoSecond);
443 pSglInfo->pBitmap = bitmap.release();
444 _EvasObjectImageChange(pSglInfo);
446 BufferInfo bufferInfo;
447 pSglInfo->pBitmap->Lock(bufferInfo);
448 pSglInfo->pBitmap->Unlock();
450 if (needSglMakeCurrent)
452 _SglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
456 eglMakeCurrent(eglDisplay, drawSurface, readSurface, eglContext);
463 _OnDestroyed(void* pData)
465 _SglInfo* pSglInfo = dynamic_cast<_SglInfo*> ((_SglInfo*)pData);
467 if (pSglInfo != null)
469 pSglInfo->pVisualElement = null;
471 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
473 pSglInfo->pSecondSglInfo->pVisualElement = null;
478 class _SglInfoTableManipulator
481 _SglInfoTableManipulator()
483 result r = __SglMutex.Create();
484 SysTryLog(NID_GRP, r == E_SUCCESS, "Failed to create mutex. [%s]", GetErrorMessage(r));
485 __SglIndexLastUsed = INVALID_SGL_INDEX;
486 __SglInitialized = false;
489 ~_SglInfoTableManipulator()
494 FindNextEmptySlotOfSglInfoTable()
496 if (!__SglInitialized)
498 if (CreateSglInfoTable() == false)
500 return INVALID_SGL_INDEX;
504 _SglIndex sglIndex = INVALID_SGL_INDEX;
505 _SglIndex index = __SglIndexLastUsed;
507 __SglMutex.Acquire();
508 for (int i = 1; i < MAX_SGL_INDEX; i++)
512 if (index >= MAX_SGL_INDEX)
517 if (__SglInfoTable[index] == null)
520 __SglIndexLastUsed = index;
524 __SglMutex.Release();
527 if (sglIndex <= INVALID_SGL_INDEX)
529 return INVALID_SGL_INDEX;
540 __SglMutex.Acquire();
542 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
544 __SglInfoTable[i] = null;
547 __SglInfoTable[0] = new (std::nothrow) _SglInfo;
548 if (__SglInfoTable[0] != null)
551 __SglInitialized = true;
552 __SglInfoTable[0]->glVersion = 2;
555 __SglMutex.Release();
561 DestroySglInfoTable()
563 __SglMutex.Acquire();
565 for (int i = 0 ; i < MAX_SGL_INDEX; i++)
567 if (__SglInfoTable[i] != null)
569 delete __SglInfoTable[i];
570 __SglInfoTable[i] = null;
573 __SglInitialized = false;
575 __SglMutex.Release();
579 LockSglInfoTable(_SglIndex index)
581 __SglMutex.Acquire();
583 if (index < INVALID_SGL_INDEX || index >= MAX_SGL_INDEX)
588 return __SglInfoTable[index];
594 __SglMutex.Release();
600 _SglInfo* newSglInfo = new (std::nothrow) _SglInfo;
601 if (newSglInfo == null)
603 SysLog(NID_GRP, "CreateSglIndex fail to allocate _SglInfo!!");
604 return INVALID_SGL_INDEX;
607 int sglIndex = FindNextEmptySlotOfSglInfoTable();
608 if (sglIndex == INVALID_SGL_INDEX)
612 SysLog(NID_GRP, "CreateSglIndex failed!!");
613 return INVALID_SGL_INDEX;
616 __SglMutex.Acquire();
617 __SglInfoTable[sglIndex] = newSglInfo;
618 __SglMutex.Release();
624 DestroySglIndex(_SglIndex sglIndex)
626 if (sglIndex < 1 || sglIndex >= MAX_SGL_INDEX)
628 SysLog(NID_GRP, "sglIndex:%#x is out of range.", (unsigned int)sglIndex);
632 __SglMutex.Acquire();
634 if (sglIndex <= INVALID_SGL_INDEX || __SglInfoTable[sglIndex] == null)
636 __SglMutex.Release();
638 SysLog(NID_GRP, "DestroySglIndex failed!! sglIndex:%#x", (unsigned int)sglIndex);
642 delete __SglInfoTable[sglIndex];
643 __SglInfoTable[sglIndex] = null;
645 __SglMutex.Release();
651 GetSglIndexForSurface(EGLSurface surface)
653 __SglMutex.Acquire();
656 for (int i = 0; i < MAX_SGL_INDEX; i++)
658 if (__SglInfoTable[i] != null && __SglInfoTable[i]->surface == surface)
665 __SglMutex.Release();
670 static _SglInfoTableManipulator* GetInstance(void)
672 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
676 pthread_once(&once_block, __InitSglInfoTableManipulator);
679 return __pTheInstance;
683 static void __InitSglInfoTableManipulator(void)
685 static _SglInfoTableManipulator instance;
686 __pTheInstance = &instance;
690 Runtime::Mutex __SglMutex;
691 _SglInfo* __SglInfoTable[MAX_SGL_INDEX];
692 _SglIndex __SglIndexLastUsed;
693 bool __SglInitialized;
695 static _SglInfoTableManipulator* __pTheInstance;
697 _SglInfoTableManipulator* _SglInfoTableManipulator::__pTheInstance = null;
700 _PixmapSurfaceDestroyCallback(void* pCallbackParam)
702 delete (_PixmapInfo*)pCallbackParam;
705 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
707 _PixmapLockCallBack(void* pCallbackParam)
709 tbm_bo_map((tbm_bo)pCallbackParam, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
713 _PixmapUnlockCallBack(void* pCallbackParam)
715 tbm_bo_unmap((tbm_bo)pCallbackParam);
720 _GetEvasEngineType(Evas_Object* pObject)
724 const char* pString = elm_config_preferred_engine_get();
725 String engineName(pString);
726 SysLog(NID_GRP, "evas backend : %s", pString);
728 if (engineName.Contains(String("opengl_x11")))
730 return ENGINE_TYPE_OPENGL_X11;
732 else if (engineName.Contains(String("software_x11")))
734 return ENGINE_TYPE_SOFEWARE_X11;
738 pString = elm_config_engine_get();
739 String engineName(pString);
740 SysLog(NID_GRP, "system evas backend : %s", pString);
741 if (engineName.Contains(String("opengl_x11")))
743 return ENGINE_TYPE_OPENGL_X11;
747 return ENGINE_TYPE_SOFEWARE_X11;
753 Evas* pEvas = evas_object_evas_get(pObject);
756 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
757 return ENGINE_TYPE_SOFEWARE_X11;
760 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
761 if (pEcoreEvas == null)
763 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEcoreEvas failed.");
764 return ENGINE_TYPE_SOFEWARE_X11;
767 const char* pString = ecore_evas_engine_name_get(pEcoreEvas);
768 String engineName(pString);
769 SysLog(NID_GRP, "evas backend : %s", pString);
771 if (engineName.Contains(String("opengl_x11")))
773 return ENGINE_TYPE_OPENGL_X11;
775 else if (engineName.Contains(String("software_x11")))
777 return ENGINE_TYPE_SOFEWARE_X11;
781 return ENGINE_TYPE_ETC;
787 _EvasObjectImageChange(_SglInfo* pSglInfo)
789 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
791 Evas* pEvas = evas_object_evas_get(pSglInfo->pObject);
794 SysLogException(NID_GRP, E_OPERATION_FAILED, "Getting pEvas failed.");
798 Evas_Native_Surface nativeSurface = {0, };
799 nativeSurface.type = EVAS_NATIVE_SURFACE_X11;
800 nativeSurface.version = EVAS_NATIVE_SURFACE_VERSION;
801 nativeSurface.data.x11.pixmap = (unsigned long)pSglInfo->nativePixmap;
802 nativeSurface.data.x11.visual = ecore_x_default_visual_get(ecore_x_display_get(), ecore_x_default_screen_get());
803 evas_object_image_native_surface_set(pSglInfo->pObject, &nativeSurface);
807 BufferInfo bufferInfo;
808 pSglInfo->pBitmap->Lock(bufferInfo);
809 evas_object_image_data_set(pSglInfo->pObject, bufferInfo.pPixels);
810 pSglInfo->pBitmap->Unlock();
814 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
816 _PostRenderCallback(Ecore_Evas* ee)
818 if (__pPreviousSglInfo != null)
820 EGLDisplay display = __pPreviousSglInfo->display;
821 EGLSurface surface = __pPreviousSglInfo->surface;
822 EGLContext context = __pPreviousSglInfo->context;
824 if (__isDoublePixmapEnabled)
826 _SglInfo* pBackSglInfo = null;
827 _SglInfo* pFrontSglInfo = null;
829 if (__pPreviousSglInfo->isBackbuffer)
831 pBackSglInfo = __pPreviousSglInfo;
832 pFrontSglInfo = __pPreviousSglInfo->pSecondSglInfo;
836 pBackSglInfo = __pPreviousSglInfo->pSecondSglInfo;
837 pFrontSglInfo = __pPreviousSglInfo;
840 if (!pBackSglInfo->wasShown && __pPreviousSglInfo->pVisualElement != null)
842 __pPreviousSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
843 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
844 evas_object_hide(pBackSglInfo->pObject);
846 pBackSglInfo->wasShown = true;
847 pFrontSglInfo->swapDone = true;
850 surface = pBackSglInfo->surface;
853 EGLBoolean ret = eglMakeCurrent(display, surface, surface, context);
854 SysTryLog(NID_GRP, ret == EGL_TRUE, "fail to restore previous surface and context. %#x %#x %#x %#x egl error:%#x"
855 , (unsigned int)display
856 , (unsigned int)surface
857 , (unsigned int)surface
858 , (unsigned int)context
859 , (unsigned int)eglGetError());
864 _SaveCurrentContext(_SglInfo* pSglInfo)
866 __pPreviousSglInfo = pSglInfo;
870 _RegisterRenderCallback(Evas_Object* pObject)
872 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11 && pObject != null)
874 if (__registerCallbackCount == 0)
876 Evas* pEvas = evas_object_evas_get(pObject);
882 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
883 if (pEcoreEvas == null)
888 ecore_evas_callback_post_render_set(pEcoreEvas, _PostRenderCallback);
890 __registerCallbackCount++;
895 _UnregisterRenderCallback(Evas_Object* pObject)
897 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11 && pObject != null)
899 __registerCallbackCount--;
901 if (__registerCallbackCount == 0)
903 Evas* pEvas = evas_object_evas_get(pObject);
909 Ecore_Evas* pEcoreEvas = ecore_evas_ecore_evas_get(pEvas);
910 if (pEcoreEvas == null)
915 ecore_evas_callback_post_render_set(pEcoreEvas, NULL);
919 #endif //#if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
931 return eglGetError();
935 _SglGetDisplay(EGLNativeDisplayType displayId)
937 if (displayId == (EGLNativeDisplayType) EGL_DEFAULT_DISPLAY)
939 return eglGetDisplay((EGLNativeDisplayType) ecore_x_display_get());
942 return eglGetDisplay((EGLNativeDisplayType) displayId);
946 _SglInitialize(EGLDisplay dpy, EGLint* pMajor, EGLint* pMinor)
948 return eglInitialize(dpy, pMajor, pMinor);
952 _SglTerminate(EGLDisplay dpy)
954 _GlesInterfaceTerminate_1();
955 _GlesInterfaceTerminate_2();
957 _SglInfoTableManipulator::GetInstance()->DestroySglInfoTable();
959 if (!__isDoublePixmapEnabled)
964 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
965 __registerCallbackCount = 0;
966 __pPreviousSglInfo = null;
969 __pPreviousSurface = EGL_NO_SURFACE;
970 __pPreviousContext = EGL_NO_CONTEXT;
976 _SglQueryString(EGLDisplay dpy, EGLint name)
978 return eglQueryString(dpy, name);
982 _SglGetConfigs(EGLDisplay dpy, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
984 return eglGetConfigs(dpy, pConfigs, configSize, pNumConfig);
988 _SglChooseConfig(EGLDisplay dpy, const EGLint* pAttribList, EGLConfig* pConfigs, EGLint configSize, EGLint* pNumConfig)
990 return eglChooseConfig(dpy, pAttribList, pConfigs, configSize, pNumConfig);
994 _SglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* pValue)
996 return eglGetConfigAttrib(dpy, config, attribute, pValue);
1000 _SglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* pAttribList)
1002 _SglIndex sglIndex = INVALID_SGL_INDEX;
1003 Object* pObj = (Object*)win;
1004 VisualElement* pVisualElement = dynamic_cast<VisualElement*> (pObj);
1005 Tizen::Ui::Control* pControl = dynamic_cast<Tizen::Ui::Control*> (pObj);
1006 #if 0//!defined(_OSP_EMUL_)
1007 Tizen::Ui::Controls::Frame* pFrame = dynamic_cast<Tizen::Ui::Controls::Frame*> (pControl);
1009 SysTryReturn(NID_GRP, pControl != null || pVisualElement != null, EGL_NO_SURFACE, E_INVALID_ARG, "Invalid NativeWindow.");
1010 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1012 #if 0//!defined(_OSP_EMUL_)
1013 if (pFrame != null && _GetEvasEngineType(null) != ENGINE_TYPE_OPENGL_X11)
1015 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(*pControl);
1016 _WindowImpl* pWindowImpl = dynamic_cast<_WindowImpl*> (pControlImpl);
1017 SysTryReturn(NID_GRP, pWindowImpl != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window state.");
1019 EGLNativeWindowType nativeWindow = (EGLNativeWindowType)pWindowImpl->GetNativeHandle();
1020 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1021 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid SglIndex.");
1023 EGLSurface windowSurface = eglCreateWindowSurface(dpy, config, nativeWindow, pAttribList);
1024 if (windowSurface == EGL_NO_SURFACE)
1026 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1031 VisualElement* pVisualElement = pControlImpl->GetCore().GetVisualElement();
1032 if (pVisualElement == null)
1034 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1035 return EGL_NO_SURFACE;
1038 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1039 if (pVisualElementImpl == null)
1041 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1042 return EGL_NO_SURFACE;
1045 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1047 pSglInfo->surface = windowSurface;
1049 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1052 return (EGLSurface)sglIndex;
1055 #endif //#if !defined(_OSP_EMUL_)
1057 result r = E_SUCCESS;
1059 if (pVisualElement == null)
1061 pVisualElement = pControl->GetVisualElement();
1063 SysTryReturn(NID_GRP, pVisualElement != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1065 _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*pVisualElement);
1066 SysTryReturn(NID_GRP, pVisualElementImpl != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1068 FloatRectangle rect = pVisualElement->GetBounds();
1069 int width = int(rect.width);
1070 int height = int(rect.height);
1071 SysTryReturn(NID_GRP, width > 0 && height > 0, EGL_NO_SURFACE, E_OUT_OF_RANGE, "Invalid size. w:%d h:%d", width, height);
1073 Control* pFrame = pControl;
1074 Control* pTemp = pFrame->GetParent();
1075 while(pTemp != null)
1078 pTemp = pTemp->GetParent();
1081 Tizen::Ui::Window* pWindow = dynamic_cast<Tizen::Ui::Window*> (pFrame);
1082 SysTryReturn(NID_GRP, pWindow != null, EGL_NO_SURFACE, E_INVALID_STATE, "Invalid window.");
1084 VisualElementSurface* pVisualElementSurface = pVisualElement->GetSurfaceN();
1085 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1087 _VisualElementSurfaceImpl* pVisualElementSurfaceImpl = _VisualElementSurfaceImpl::GetInstance(*pVisualElementSurface);
1088 if (pVisualElementSurfaceImpl == null)
1090 SysLogException(NID_GRP, E_OPERATION_FAILED, "Propagating.");
1091 delete pVisualElementSurface;
1092 return EGL_NO_SURFACE;
1095 Evas_Object* pObject = (Evas_Object*)pVisualElementSurfaceImpl->GetNativeHandle();
1096 delete pVisualElementSurface;
1098 Evas* pEvas = evas_object_evas_get(pObject);
1099 pObject = evas_object_image_filled_add(pEvas);
1100 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1102 int widthPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalX(rect.width));
1103 int heightPhysical = static_cast<int> (CoordinateSystem::ConvertToPhysicalY(rect.height));
1105 evas_object_image_alpha_set(pObject, EINA_TRUE);
1106 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1107 evas_object_resize(pObject, widthPhysical, heightPhysical);
1108 evas_object_hide(pObject);
1110 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1111 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1112 SysTryReturn(NID_GRP, pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1114 std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
1115 SysTryReturn(NID_GRP, bitmap.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1117 r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
1118 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1120 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmap.get(), null);
1121 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1123 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1124 pSglInfo->sglIndex = sglIndex;
1125 pSglInfo->display = dpy;
1126 pSglInfo->config = config;
1127 pSglInfo->pVisualElement = pVisualElement;
1128 pSglInfo->pWindow = pWindow;
1129 pSglInfo->pObject = pObject;
1130 pSglInfo->pVisualElementSurface = pVisualElementSurface;
1131 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1133 __evasEngineType = _GetEvasEngineType(pObject);
1135 #if defined (FGRAPHICS_INTERNAL_USE_DOUBLE_PIXMAP)
1136 if (__evasEngineType == ENGINE_TYPE_OPENGL_X11)
1138 __isDoublePixmapEnabled = true;
1142 __isDoublePixmapEnabled = false;
1145 __isDoublePixmapEnabled = false;
1148 if (__isDoublePixmapEnabled)
1150 pObject = evas_object_image_filled_add(pEvas);
1151 SysTryReturn(NID_GRP, pObject != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Invalid object.");
1153 evas_object_image_alpha_set(pObject, EINA_TRUE);
1154 evas_object_image_size_set(pObject, widthPhysical, heightPhysical);
1155 evas_object_resize(pObject, widthPhysical, heightPhysical);
1156 evas_object_hide(pObject);
1158 pVisualElementSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(
1159 *pWindow->GetDisplayContext(), (Handle)pObject, Dimension(width, height));
1160 SysTryReturn(NID_GRP, pSglInfo->pVisualElementSurface != null, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1162 delete pSglInfo->pBitmap;
1163 pSglInfo->pBitmap = null;
1165 std::auto_ptr <Bitmap> bitmapSecond(new (std::nothrow) Bitmap);
1166 SysTryReturn(NID_GRP, bitmapSecond.get() != null, EGL_NO_SURFACE, E_OUT_OF_MEMORY, "The memory is insufficient.");
1168 r = bitmapSecond.get()->Construct(Rectangle(0, 0, width, height));
1169 SysTryReturn(NID_GRP, r == E_SUCCESS, EGL_NO_SURFACE, r, "Propagating.");
1171 sglIndex = (_SglIndex)_SglCreatePixmapSurface(dpy, config, (EGLNativePixmapType)bitmapSecond.get(), null);
1172 SysTryReturn(NID_GRP, sglIndex > INVALID_SGL_INDEX, EGL_NO_SURFACE, E_OPERATION_FAILED, "Propagating.");
1174 _SglInfo* pSglInfoSecond = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1175 pSglInfoSecond->sglIndex = sglIndex;
1176 pSglInfoSecond->display = dpy;
1177 pSglInfoSecond->config = config;
1178 pSglInfoSecond->pVisualElement = pVisualElement;
1179 pSglInfoSecond->pWindow = pWindow;
1180 pSglInfoSecond->pObject = pObject;
1181 pSglInfoSecond->pVisualElementSurface = pVisualElementSurface;
1182 pSglInfoSecond->swapDone = true;
1183 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1185 pSglInfo->pSecondSglInfo = pSglInfoSecond;
1186 pSglInfoSecond->pSecondSglInfo = pSglInfo;
1188 pSglInfoSecond->pBitmap = bitmapSecond.release();
1189 _EvasObjectImageChange(pSglInfoSecond);
1193 pVisualElement->SetSurface(pSglInfo->pVisualElementSurface);
1196 pSglInfo->pBitmap = bitmap.release();
1197 _EvasObjectImageChange(pSglInfo);
1199 pVisualElementImpl->SetBoundsChangedCallback(_OnBoundsChanged, pSglInfo);
1200 pVisualElementImpl->SetDestroyedCallback(_OnDestroyed, pSglInfo);
1201 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1202 _RegisterRenderCallback(pSglInfo->pObject);
1204 return (EGLSurface)pSglInfo->sglIndex;
1209 _SglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* pAttribList)
1211 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1212 _SglIndex sglIndex = INVALID_SGL_INDEX;
1214 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1215 if (sglIndex <= INVALID_SGL_INDEX)
1217 SysLog(NID_GRP, "_SglCreatePbufferSurface failed!! dpy:%#x config:%#x pAttribList:%#x",
1218 (unsigned int)dpy, (unsigned int)config, (unsigned int)pAttribList);
1220 return eglCreatePbufferSurface(EGL_NO_DISPLAY, config, pAttribList);
1223 EGLSurface pbufferSurface = eglCreatePbufferSurface(dpy, config, pAttribList);
1224 if (pbufferSurface == EGL_NO_SURFACE)
1226 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1231 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1233 pSglInfo->surface = pbufferSurface;
1235 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1238 return (EGLSurface)sglIndex;
1241 _OSP_LOCAL_ EGLNativePixmapType
1242 _CreateNativePixmap(_SglIndex sglIndex, Tizen::Graphics::Bitmap* pBitmap)
1246 int bitsPerPixel = 0;
1247 Display* pNativeDisplay = null;
1248 Drawable nativeWindow = 0;
1249 result r = E_FAILURE;
1250 Tizen::Graphics::BufferInfo bufferInfo;
1251 _PixmapInfo* pPixmapInfo = null;
1252 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1253 unsigned int attachments[] = { DRI2BufferFrontLeft };
1254 char* pDeviceName = null;
1255 char* pDriverName = null;
1256 DRI2Buffer* pDri2Buffer = null;
1259 int dri2BufferCount = 0;
1260 tbm_bo_handle tbmData = { null, };
1263 drm_magic_t magic = 0;
1265 _SglInfo* pSglInfo = null;
1266 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1268 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1269 if (pBitmap == null || sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1271 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1274 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1276 r = pBitmap->Lock(bufferInfo);
1282 width = bufferInfo.width;
1283 height = bufferInfo.height;
1284 bitsPerPixel = bufferInfo.bitsPerPixel;
1286 r = pBitmap->Unlock();
1287 if (r != E_SUCCESS || width <= 0 || height <= 0 || bitsPerPixel <= 0)
1292 pNativeDisplay = (Display*) ecore_x_display_get();
1293 nativeWindow = DefaultRootWindow(pNativeDisplay);
1295 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1296 if (pPixmapInfo == null)
1301 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height,
1302 ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1303 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1308 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1309 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1310 XSync(pNativeDisplay, False);
1312 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1314 if (!ret || pDeviceName == null)
1319 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1322 if (pPixmapInfo->drmFd < 0)
1327 drmGetMagic(pPixmapInfo->drmFd, &magic);
1328 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1334 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1335 if (pPixmapInfo->pTbmBufMgr == null)
1340 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1341 if (pDri2Buffer == null)
1346 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1348 if (pPixmapInfo->pTbmBufferObject == null)
1353 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1354 if (tbmData.ptr == null)
1359 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, tbmData.ptr, width * bitsPerPixel / 8,
1360 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1366 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap, _PixmapSurfaceDestroyCallback, pPixmapInfo,
1367 _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1373 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1375 pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1377 pSglInfo->pEcoreImage = ecore_x_image_new(width
1379 , ecore_x_default_visual_get(pNativeDisplay, ecore_x_default_screen_get())
1380 , ecore_x_default_depth_get(pNativeDisplay, ecore_x_default_screen_get()));
1382 if (pSglInfo->pEcoreImage == null)
1384 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1388 ecore_x_image_get(pSglInfo->pEcoreImage
1389 , pPixmapInfo->nativePixmap, 0, 0, 0, 0
1397 void* pSource = ecore_x_image_data_get(pSglInfo->pEcoreImage, &bpl, &rows, &bpp);
1399 r = Tizen::Graphics::_BitmapTool::ChangeBuffer(*pBitmap, pSource, width * bitsPerPixel / 8,
1400 _PixmapSurfaceDestroyCallback, (void*)pPixmapInfo);
1403 ecore_x_image_free(pSglInfo->pEcoreImage);
1404 pSglInfo->pEcoreImage = null;
1405 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1407 SysLog(NID_GRP, "change buffer failed! pSource %#x %d %d %d", (unsigned int)pSource, bpl, rows, bpp);
1411 pSglInfo->width = width;
1412 pSglInfo->height = height;
1414 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1416 #endif //#if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
1417 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1419 pBitmap->Lock(bufferInfo);
1420 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1423 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1426 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1427 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1428 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1432 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1436 close(pPixmapInfo->drmFd);
1440 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1442 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1450 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1451 return (EGLNativePixmapType)0;
1454 _OSP_LOCAL_ EGLNativePixmapType
1455 _CreateNativePixmapEx(Tizen::Graphics::Bitmap* pBitmap, Tizen::Graphics::BufferInfo bufferInfo)
1459 int bitsPerPixel = 0;
1460 Display* pNativeDisplay = null;
1461 Drawable nativeWindow = 0;
1462 Tizen::Graphics::_BitmapImpl* pBitmapImpl = null;
1463 _PixmapInfo* pPixmapInfo = null;
1464 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1465 unsigned int attachments[] = { DRI2BufferFrontLeft };
1466 char* pDeviceName = null;
1467 char* pDriverName = null;
1468 DRI2Buffer* pDri2Buffer = null;
1471 int dri2BufferCount = 0;
1472 tbm_bo_handle tbmData = { null, };
1475 result r = E_FAILURE;
1476 drm_magic_t magic = 0;
1478 if (pBitmap == null)
1483 pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(*pBitmap);
1484 if (pBitmapImpl == null)
1489 width = bufferInfo.width;
1490 height = bufferInfo.height;
1491 bitsPerPixel = bufferInfo.bitsPerPixel;
1493 if (width <= 0 || height <= 0 || bitsPerPixel <= 0)
1498 pNativeDisplay = (Display*) ecore_x_display_get();
1499 nativeWindow = DefaultRootWindow(pNativeDisplay);
1501 pPixmapInfo = new (std::nothrow) _PixmapInfo;
1502 if (pPixmapInfo == null)
1507 pPixmapInfo->nativePixmap = XCreatePixmap(pNativeDisplay, nativeWindow, width, height
1509 if (pPixmapInfo->nativePixmap == (Pixmap)0)
1514 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1515 DRI2CreateDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1516 XSync(pNativeDisplay, False);
1518 ret = DRI2Connect(pNativeDisplay, nativeWindow, &pDriverName, &pDeviceName);
1520 if (!ret || pDeviceName == null)
1525 pPixmapInfo->drmFd = open(pDeviceName, O_RDWR);
1527 if (pPixmapInfo->drmFd < 0)
1532 drmGetMagic(pPixmapInfo->drmFd, &magic);
1533 ret = DRI2Authenticate(pNativeDisplay, nativeWindow, (unsigned int)magic);
1539 pPixmapInfo->pTbmBufMgr = tbm_bufmgr_init(pPixmapInfo->drmFd);
1540 if (pPixmapInfo->pTbmBufMgr == null)
1545 pDri2Buffer = DRI2GetBuffers(pNativeDisplay, pPixmapInfo->nativePixmap, &dri2Width, &dri2Height, attachments, 1, &dri2BufferCount);
1546 if (pDri2Buffer == null)
1551 pPixmapInfo->pTbmBufferObject = tbm_bo_import(pPixmapInfo->pTbmBufMgr, pDri2Buffer->name);
1553 if (pPixmapInfo->pTbmBufferObject == null)
1558 tbmData = tbm_bo_get_handle(pPixmapInfo->pTbmBufferObject, TBM_DEVICE_CPU);
1559 if (tbmData.ptr == null)
1563 bufferInfo.pPixels = tbmData.ptr;
1565 bufferInfo.bitsPerPixel = 32;
1566 bufferInfo.pixelFormat = PIXEL_FORMAT_ARGB8888;
1567 bufferInfo.pitch = bufferInfo.width * bufferInfo.bitsPerPixel / 8;
1568 memset(bufferInfo.pPixels, 0, bufferInfo.pitch * bufferInfo.height);
1569 r = pBitmapImpl->Construct(bufferInfo);
1575 if (!Tizen::Graphics::_BitmapTool::SetCallback(*pBitmap
1576 , _PixmapSurfaceDestroyCallback, pPixmapInfo
1577 , _PixmapLockCallBack, pPixmapInfo->pTbmBufferObject
1578 ,_PixmapUnlockCallBack, pPixmapInfo->pTbmBufferObject))
1582 #endif //#if defined(FGRAPHICS_INTERNAL_USE_DRM)
1584 return (EGLNativePixmapType)pPixmapInfo->nativePixmap;
1586 #if defined(FGRAPHICS_INTERNAL_USE_DRM)
1588 tbm_bo_unmap(pPixmapInfo->pTbmBufferObject);
1589 tbm_bo_unref(pPixmapInfo->pTbmBufferObject);
1593 tbm_bufmgr_deinit(pPixmapInfo->pTbmBufMgr);
1597 close(pPixmapInfo->drmFd);
1601 DRI2DestroyDrawable(pNativeDisplay, pPixmapInfo->nativePixmap);
1602 XFreePixmap(pNativeDisplay, pPixmapInfo->nativePixmap);
1611 SysLog(NID_GRP, "_CreateNativePixmap failed!! pBitmap:%#x", (unsigned int)pBitmap);
1612 return (EGLNativePixmapType)0;
1616 _SglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint* pAttribList)
1618 _SglIndex sglIndex = INVALID_SGL_INDEX;
1619 Tizen::Graphics::Bitmap* pBitmap = dynamic_cast<Tizen::Graphics::Bitmap*>((Tizen::Graphics::Bitmap*)pixmap);
1620 EGLNativePixmapType eglNativePixmap = 0;
1621 EGLSurface pixmapSurface = EGL_NO_SURFACE;
1622 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1624 if (pBitmap == null)
1629 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1630 if (sglIndex <= INVALID_SGL_INDEX)
1635 eglNativePixmap = _CreateNativePixmap(sglIndex, pBitmap);
1636 if (eglNativePixmap == 0)
1641 pixmapSurface = eglCreatePixmapSurface(dpy, config, eglNativePixmap, pAttribList);
1642 if (pixmapSurface == EGL_NO_SURFACE)
1644 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1649 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1651 pSglInfo->nativePixmap = eglNativePixmap;
1652 pSglInfo->surface = pixmapSurface;
1654 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1657 return (EGLSurface)sglIndex;
1660 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1664 SysLog(NID_GRP, "_SglCreatePixmapSurface failed!! dpy:%#x config:%#x pixmap:%#x pAttribList:%#x",
1665 (unsigned int)dpy, (unsigned int)config, (unsigned int)pixmap, (unsigned int)pAttribList);
1666 return eglCreatePixmapSurface(EGL_NO_DISPLAY, config, (EGLNativePixmapType) 0, pAttribList);
1670 _SglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1672 if (__pPreviousSurface == surface)
1674 __pPreviousSurface = EGL_NO_SURFACE;
1677 EGLBoolean ret = EGL_FALSE;
1678 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1680 _SglIndex sglIndex = (_SglIndex)surface;
1681 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1683 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1685 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1686 SysLog(NID_GRP, "_SglDestroySurface failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
1687 , (unsigned int)surface);
1688 return eglDestroySurface(dpy, EGL_NO_SURFACE);
1691 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1692 if (pSglInfo == __pPreviousSglInfo || pSglInfo->pSecondSglInfo == __pPreviousSglInfo)
1694 __pPreviousSglInfo = null;
1698 ret = eglDestroySurface(dpy, pSglInfo->surface);
1699 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1701 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
1703 eglDestroySurface(dpy, pSglInfo->pSecondSglInfo->surface);
1704 pSglInfoTableManipulatorInstance->DestroySglIndex(pSglInfo->pSecondSglInfo->sglIndex);
1707 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1713 _SglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint* pValue)
1715 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1717 _SglIndex sglIndex = (_SglIndex)surface;
1718 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1720 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1722 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1723 SysLog(NID_GRP, "_SglQuerySurface failed!! dpy:%#x sglIndex:%#x attribute:%#x pValue:%#x",
1724 (unsigned int)dpy, (unsigned int)sglIndex, (unsigned int)attribute, (unsigned int)pValue);
1725 return eglQuerySurface(dpy, EGL_NO_SURFACE, attribute, pValue);
1728 EGLBoolean ret = eglQuerySurface(dpy, pSglInfo->surface, attribute, pValue);
1729 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1735 _SglBindAPI(EGLenum api)
1737 return eglBindAPI(api);
1743 return eglQueryAPI();
1749 return eglWaitClient();
1753 _SglReleaseThread(void)
1755 return eglReleaseThread();
1759 _SglCreatePbufferFromClientBuffer(
1760 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint* pAttribList)
1762 _SglIndex sglIndex = INVALID_SGL_INDEX;
1763 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1765 sglIndex = pSglInfoTableManipulatorInstance->CreateSglIndex();
1766 if (sglIndex <= INVALID_SGL_INDEX)
1768 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!!"
1769 " dpy:%#x buftype:%#x buffer:%#x config:%#x pAttribList:%#x",
1770 (unsigned int)dpy, (unsigned int)buftype, (unsigned int)buffer, (unsigned int)config, (unsigned int)pAttribList);
1772 return eglCreatePbufferFromClientBuffer(null, (EGLenum)0, null, null, null);
1775 EGLSurface pbufferFromClientBuffer = eglCreatePbufferFromClientBuffer(dpy, buftype, buffer, config, pAttribList);
1776 if (pbufferFromClientBuffer == EGL_NO_SURFACE)
1778 pSglInfoTableManipulatorInstance->DestroySglIndex(sglIndex);
1783 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1785 pSglInfo->surface = pbufferFromClientBuffer;
1787 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1790 return (EGLSurface)sglIndex;
1794 _SglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1796 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1798 _SglIndex sglIndex = (_SglIndex)surface;
1799 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1801 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1803 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1804 SysLog(NID_GRP, "_SglCreatePbufferFromClientBuffer failed!! "
1805 "dpy:%#x surface:%#x attribute:%#x value:%#x",
1806 (unsigned int)dpy, (unsigned int)surface, (unsigned int)attribute, (unsigned int)value);
1808 return eglSurfaceAttrib(dpy, EGL_NO_SURFACE, attribute, value);
1811 EGLBoolean ret = eglSurfaceAttrib(dpy, pSglInfo->surface, attribute, value);
1812 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1818 _SglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1820 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1822 _SglIndex sglIndex = (_SglIndex)surface;
1823 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1825 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1827 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1828 SysLog(NID_GRP, "_SglBindTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1829 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1831 return eglBindTexImage(dpy, EGL_NO_SURFACE, buffer);
1834 EGLBoolean ret = eglBindTexImage(dpy, pSglInfo->surface, buffer);
1835 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1841 _SglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1843 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1845 _SglIndex sglIndex = (_SglIndex)surface;
1846 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
1848 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
1850 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1851 SysLog(NID_GRP, "_SglReleaseTexImage failed!! dpy:%#x surface:%#x buffer:%#x",
1852 (unsigned int)dpy, (unsigned int)surface, (unsigned int)buffer);
1854 return eglReleaseTexImage(dpy, EGL_NO_SURFACE, buffer);
1857 EGLBoolean ret = eglReleaseTexImage(dpy, pSglInfo->surface, buffer);
1858 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1864 _SglSwapInterval(EGLDisplay dpy, EGLint interval)
1866 return eglSwapInterval(dpy, interval);
1870 _SglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareContext, const EGLint* pAttribList)
1872 return eglCreateContext(dpy, config, shareContext, pAttribList);
1876 _SglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1878 if (__pPreviousContext == ctx)
1880 __pPreviousContext = EGL_NO_CONTEXT;
1883 return eglDestroyContext(dpy, ctx);
1887 _SglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1889 if (__pPreviousSurface == draw && __pPreviousContext == ctx)
1893 __pPreviousSurface = draw;
1894 __pPreviousContext = ctx;
1896 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
1898 _SglIndex sglIndexDraw = (_SglIndex)draw;
1899 _SglIndex sglIndexRead = (_SglIndex)read;
1901 EGLSurface sglInfoReadSurface;
1903 _SglInfo* sglInfoRead = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexRead);
1905 if (sglInfoRead == 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);
1912 sglInfoReadSurface = sglInfoRead->surface;
1914 if (__isDoublePixmapEnabled && !sglInfoRead->isBackbuffer && sglInfoRead->pSecondSglInfo != null)
1916 sglInfoReadSurface = sglInfoRead->pSecondSglInfo->surface;
1919 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1922 _SglInfo* pSglInfoDraw = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndexDraw);
1923 if (pSglInfoDraw == null)
1925 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1926 SysLog(NID_GRP, "_SglMakeCurrent failed!! dpy:%#x draw:%#x read:%#x ctx:%#x",
1927 (unsigned int)dpy, (unsigned int)draw, (unsigned int)read, (unsigned int)ctx);
1928 return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
1931 if (__isDoublePixmapEnabled && !pSglInfoDraw->isBackbuffer && pSglInfoDraw->pSecondSglInfo != null)
1933 pSglInfoDraw = pSglInfoDraw->pSecondSglInfo;
1936 pSglInfoDraw->context = ctx;
1938 EGLBoolean ret = eglMakeCurrent(dpy, pSglInfoDraw->surface, sglInfoReadSurface, ctx);
1940 #if defined(FGRAPHICS_INTERNAL_USE_RESTORE_CONTEXT)
1941 if (draw != EGL_NO_SURFACE)
1943 _SaveCurrentContext(pSglInfoDraw);
1947 if (pSglInfoDraw->glVersion == 0 && ctx != EGL_NO_CONTEXT)
1949 eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &pSglInfoDraw->glVersion);
1952 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
1958 _SglGetCurrentContext()
1960 return eglGetCurrentContext();
1964 _SglGetCurrentSurface(EGLint readDraw)
1966 EGLSurface surface = EGL_NO_SURFACE;
1967 _SglIndex sglIndex = INVALID_SGL_INDEX;
1969 surface = eglGetCurrentSurface(readDraw);
1970 sglIndex = _SglInfoTableManipulator::GetInstance()->GetSglIndexForSurface(surface);
1972 if (sglIndex < INVALID_SGL_INDEX)
1974 SysLog(NID_GRP, "_SglGetCurrentSurface failed!! readDraw:%#x", (unsigned int)readDraw);
1975 return EGL_NO_SURFACE;
1978 return (EGLSurface)sglIndex;
1982 _SglGetCurrentDisplay(void)
1984 return eglGetCurrentDisplay();
1988 _SglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* pValue)
1990 return eglQueryContext(dpy, ctx, attribute, pValue);
2000 _SglWaitNative(EGLint engine)
2002 return eglWaitNative(engine);
2005 EGLBoolean _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface);
2008 _SglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
2010 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2012 _SglIndex sglIndex = (_SglIndex)surface;
2013 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2015 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2017 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2018 SysLog(NID_GRP, "_SglSwapBuffers failed!! dpy:%#x sglIndex:%#x", (unsigned int)dpy
2019 , (unsigned int)sglIndex);
2021 return eglSwapBuffers(dpy, EGL_NO_SURFACE);
2024 if (pSglInfo->pBitmap != null)
2026 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2027 _SglUpdateBufferOSP(dpy, surface);
2029 if(__evasEngineType == ENGINE_TYPE_OPENGL_X11)
2031 if (pSglInfo->glVersion == 1)
2046 if(__evasEngineType == ENGINE_TYPE_SOFEWARE_X11 || __evasEngineType == ENGINE_TYPE_OPENGL_X11)
2048 if (__isDoublePixmapEnabled && pSglInfo->pSecondSglInfo != null)
2050 if (pSglInfo->swapDone || pSglInfo->pSecondSglInfo->swapDone)
2052 _SglInfo* pBackSglInfo = null;
2053 _SglInfo* pFrontSglInfo = null;
2055 if (pSglInfo->isBackbuffer)
2057 pBackSglInfo = pSglInfo;
2058 pFrontSglInfo = pSglInfo->pSecondSglInfo;
2062 pBackSglInfo = pSglInfo->pSecondSglInfo;
2063 pFrontSglInfo = pSglInfo;
2065 pBackSglInfo->isBackbuffer = false;
2066 pFrontSglInfo->isBackbuffer = true;
2068 pFrontSglInfo->wasShown = false;
2069 pFrontSglInfo->swapDone = false;
2070 if (pSglInfo->pVisualElement != null)
2072 pSglInfo->pVisualElement->SetSurface(pFrontSglInfo->pVisualElementSurface);
2074 evas_object_image_pixels_dirty_set(pFrontSglInfo->pObject, EINA_TRUE);
2075 evas_object_hide(pBackSglInfo->pObject);
2077 if (pSglInfo->isFirstSwap)
2079 pBackSglInfo->isFirstSwap = false;
2080 pFrontSglInfo->isFirstSwap = false;
2083 BufferInfo bufferInfo;
2084 pFrontSglInfo->pBitmap->Lock(bufferInfo);
2085 pFrontSglInfo->pBitmap->Merge(Point(0, 0), *pBackSglInfo->pBitmap, Rectangle(0, 0, bufferInfo.width, bufferInfo.height));
2086 pFrontSglInfo->pBitmap->Unlock();
2092 if (pSglInfo->pObject != null)
2094 evas_object_image_pixels_dirty_set(pSglInfo->pObject, EINA_TRUE);
2100 if (pSglInfo->pVisualElement != null)
2102 pSglInfo->pVisualElement->SetFlushNeeded();
2107 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2112 EGLBoolean ret = eglSwapBuffers(dpy, pSglInfo->surface);
2113 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2120 _SglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
2122 Tizen::Graphics::Bitmap* pBitmap = (Tizen::Graphics::Bitmap*)target;
2123 EGLNativePixmapType nativePixmap = _CreateNativePixmap((_SglIndex)surface, pBitmap);
2124 return eglCopyBuffers(dpy, surface, nativePixmap);
2127 typedef void (*__EglMustCastToProperFunctionPointerType)(void);
2129 __EglMustCastToProperFunctionPointerType
2130 _SglGetProcAddress(const char* pProcName)
2132 return eglGetProcAddress(pProcName);
2136 _SglUpdateBufferOSP(EGLDisplay dpy, EGLSurface surface)
2138 #if defined(FGRAPHICS_INTERNAL_USE_EGLCOPYBUFFER)
2139 Tizen::Graphics::BufferInfo bufferInfo;
2140 _SglInfoTableManipulator* pSglInfoTableManipulatorInstance = _SglInfoTableManipulator::GetInstance();
2142 _SglIndex sglIndex = (_SglIndex)surface;
2143 _SglInfo* pSglInfo = pSglInfoTableManipulatorInstance->LockSglInfoTable(sglIndex);
2145 if (sglIndex <= INVALID_SGL_INDEX || pSglInfo == null)
2147 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2148 SysLog(NID_GRP, "_SglUpdateBufferOSP failed!!");
2152 eglCopyBuffers(dpy, pSglInfo->surface, pSglInfo->nativePixmap);
2153 ecore_x_image_get(pSglInfo->pEcoreImage, (Ecore_X_Drawable)pSglInfo->nativePixmap,
2154 0, 0, 0, 0, pSglInfo->width, pSglInfo->height);
2156 pSglInfoTableManipulatorInstance->UnlockSglInfoTable();
2167 }} // Tizen::Graphics