2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
19 * @file FGrp_GlPlayerImpl.cpp
20 * @brief This is the implementation file for _GlPlayerImpl class.
26 #include <FUiCtrlForm.h>
27 #include <FUiCtrlFrame.h>
28 #include <FBaseSysLog.h>
29 #include <FSysSystemTime.h>
31 #include <FGrpGlPlayer.h>
33 #include "FGrp_GlPlayerImpl.h"
35 namespace Tizen { namespace Graphics { namespace Opengl
38 _GlPlayerImpl::_GlPlayerImpl(void)
41 , __pTargetControl(null)
42 , __eglDisplay(EGL_NO_DISPLAY)
43 , __eglSurface(EGL_NO_SURFACE)
45 , __eglContext(EGL_NO_CONTEXT)
47 , __renderTimeInterval(20)
48 , __eglContextClientVersion(EGL_CONTEXT_CLIENT_VERSION_2_X)
49 , __playerState(PLAYER_STATE_NOT_INITIALIZED)
51 __eglConfigList[0] = EGL_NONE;
54 _GlPlayerImpl::~_GlPlayerImpl(void)
66 _GlPlayerImpl::__CheckReadyToStart(void)
68 return (__pTargetControl == null) ? false : true;
72 _GlPlayerImpl::__DestroyGl(void)
74 if (__eglDisplay == EGL_NO_DISPLAY)
79 if (__pRenderer != null)
81 __pRenderer->TerminateGl();
85 eglMakeCurrent(__eglDisplay, null, null, null);
86 if (__eglContext != EGL_NO_CONTEXT)
88 eglDestroyContext(__eglDisplay, __eglContext);
89 __eglContext = EGL_NO_CONTEXT;
92 if (__eglSurface != EGL_NO_SURFACE)
94 eglDestroySurface(__eglDisplay, __eglSurface);
95 __eglSurface = EGL_NO_SURFACE;
98 eglTerminate(__eglDisplay);
99 __eglDisplay = EGL_NO_DISPLAY;
105 _GlPlayerImpl::Construct(EglContextClientVersion version, Tizen::Ui::Control* pControl)
107 SysTryReturnResult(NID_GRP, version == EGL_CONTEXT_CLIENT_VERSION_1_X ||
108 version == EGL_CONTEXT_CLIENT_VERSION_2_X, E_INVALID_ARG, "The input client version is invalid");
110 Tizen::Ui::Controls::Form* pForm = dynamic_cast<Tizen::Ui::Controls::Form*> (pControl);
111 Tizen::Ui::Controls::Frame* pFrame = dynamic_cast<Tizen::Ui::Controls::Frame*> (pControl);
112 SysTryReturnResult(NID_GRP, pForm != null || pFrame != null, E_INVALID_ARG,
113 "The input target pControl is not allowable. Only Form/Frame are acceptable. ");
114 SysTryReturnResult(NID_GRP, __pTargetControl == null, E_INVALID_OPERATION,
115 "It is not allowable to call Construct() function, and the target pControl is not replacable.");
117 std::auto_ptr<Tizen::Base::Runtime::Timer> timer(new (std::nothrow) Tizen::Base::Runtime::Timer);
118 SysTryReturnResult(NID_GRP, timer.get() != null, E_OUT_OF_MEMORY, "Fail to generate Timer. ");
119 SysTryReturnResult(NID_GRP, timer->Construct(*this) == E_SUCCESS, E_OUT_OF_MEMORY, "Fail to construct Timer. ");
121 __eglContextClientVersion = version;
122 __pTargetControl = pControl;
123 __pTimer = timer.release();
129 _GlPlayerImpl::IsValid(void) const
131 return (this && this->__pTimer);
135 _GlPlayerImpl::Start(void)
138 EGLint eglContextList[3] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
140 if (__playerState == PLAYER_STATE_START)
145 if (__playerState == PLAYER_STATE_PAUSE)
147 SysTryReturnResult(NID_GRP, false, E_INVALID_STATE, "Cannot start player when the player's state is paused.");
150 SysTryReturnResult(NID_GRP, __CheckReadyToStart(), E_INVALID_STATE, "This instance is not ready to start yet.");
152 eglBindAPI(EGL_OPENGL_ES_API);
153 if (__eglDisplay != EGL_NO_DISPLAY)
158 __eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);
159 SysTryReturnResult(NID_GRP, __eglDisplay != EGL_NO_DISPLAY, E_OPENGL_ERROR, "Fail to find EGL_DEFAULT_DISPLAY device with eglGetDisplay().");
161 if (eglInitialize(__eglDisplay, null, null) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
164 SysTryReturnResult(NID_GRP, false, E_OPENGL_ERROR, "Fail to initialize egl with eglInitialize().");
167 if (eglChooseConfig(__eglDisplay, __eglConfigList, &__eglConfig, 1, &numConfigs) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
170 SysTryReturnResult(NID_GRP, false, E_OPENGL_ERROR, "Fail to choose appropriate egl configurations with eglChooseConfig().");
176 SysTryReturnResult(NID_GRP, false, E_OPENGL_ERROR, "There is no matching configuration for the given condition.");
179 __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, (EGLNativeWindowType)__pTargetControl, null);
180 if (__eglSurface == EGL_NO_SURFACE || eglGetError() != EGL_SUCCESS)
183 SysTryReturnResult(NID_GRP, false, E_OPENGL_ERROR, "Fail to create window surface with eglCreateWindowSurface().");
186 switch (__eglContextClientVersion)
188 case EGL_CONTEXT_CLIENT_VERSION_1_X:
189 eglContextList[1] = 1;
191 case EGL_CONTEXT_CLIENT_VERSION_2_X:
192 eglContextList[1] = 2;
195 __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextList);
196 if (__eglContext == EGL_NO_CONTEXT || eglGetError() != EGL_SUCCESS)
199 SysTryReturnResult(NID_GRP, false, E_OPENGL_ERROR, "Fail to create egl context with eglCreateContext().");
202 if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
205 SysTryReturnResult(NID_GRP, false, E_OPENGL_ERROR, "Fail to active the current context with eglMakeCurrent().");
210 result r = __pTimer->Start(__renderTimeInterval);
211 SysTryReturnResult(NID_GRP, r == E_SUCCESS, r, "Fail to resume Timer.");
214 __playerState = PLAYER_STATE_START;
220 _GlPlayerImpl::Pause(void)
222 if (__playerState == PLAYER_STATE_PAUSE || __playerState == PLAYER_STATE_STOP)
227 if (__playerState == PLAYER_STATE_NOT_INITIALIZED)
229 SysTryReturnResult(NID_GRP, false, E_INVALID_STATE, "Cannot pause player when the player is not started.");
232 if (__pTimer != null)
234 result r = __pTimer->Cancel();
235 SysTryReturnResult(NID_GRP, r == E_SUCCESS, r, "Fail to pause Timer.");
238 if (__pRenderer != null)
240 SysTryReturnResult(NID_GRP, __pRenderer->Pause(), E_INVALID_OPERATION, "Fail to perform IGlRenderer::Pause() function .");
243 __playerState = PLAYER_STATE_PAUSE;
249 _GlPlayerImpl::Resume(void)
251 if (__playerState == PLAYER_STATE_START)
256 if (__playerState == PLAYER_STATE_NOT_INITIALIZED || __playerState == PLAYER_STATE_STOP)
258 SysTryReturnResult(NID_GRP, false, E_INVALID_STATE, "Cannot resume player when the player is not started.");
261 if (__pRenderer != null)
263 SysTryReturnResult(NID_GRP, __pRenderer->Resume(), E_INVALID_OPERATION, "Fail to perform IGlRenderer::Resume() function .");
265 if (__pTimer != null)
267 result r = __pTimer->Start(__renderTimeInterval);
268 SysTryReturnResult(NID_GRP, r == E_SUCCESS, r, "Fail to resume Timer.");
271 __playerState = PLAYER_STATE_START;
277 _GlPlayerImpl::Stop(void)
279 if (__playerState == PLAYER_STATE_STOP || __playerState == PLAYER_STATE_NOT_INITIALIZED)
284 if (__playerState == PLAYER_STATE_PAUSE)
287 __playerState = PLAYER_STATE_STOP;
290 if (__pTimer != null)
292 result r = __pTimer->Cancel();
293 SysTryReturnResult(NID_GRP, r == E_SUCCESS, r, "Fail to pause Timer.");
296 if (__pRenderer != null)
298 SysTryReturnResult(NID_GRP, __pRenderer->Pause(), E_INVALID_OPERATION, "Fail to perform IGlRenderer::Pause() function .");
302 __playerState = PLAYER_STATE_STOP;
308 _GlPlayerImpl::Redraw(void)
310 SysTryReturnResult(NID_GRP, eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_FALSE, E_OPENGL_ERROR, "eglMakeCurrent() has been failed.");
312 if (__pRenderer != null)
315 eglSwapBuffers(__eglDisplay, __eglSurface);
322 _GlPlayerImpl::SetFps(int fps)
327 __renderTimeInterval = (int)((float)1000 / (float)fps + 0.5f);
331 __renderTimeInterval = -1;
338 _GlPlayerImpl::SetEglAttributeList(const EGLint* pEglConfigList)
342 SysTryReturnResult(NID_GRP, pEglConfigList != null, E_INVALID_ARG, "Input is invalid EGL attribute list. ");
344 while (pEglConfigList[index] != EGL_NONE)
346 SysTryReturnResult(NID_GRP, index < 38, E_INVALID_ARG, "Input eglConfigList does not have terminal symbol 'EGL_NONE'. ");
348 __eglConfigList[index] = pEglConfigList[index];
349 __eglConfigList[index+1] = pEglConfigList[index+1];
352 __eglConfigList[index] = EGL_NONE;
358 _GlPlayerImpl::SetEglAttributePreset(EglAttributesPreset preset)
364 case EGL_ATTRIBUTES_PRESET_RGB565:
365 __eglConfigList[index++] = EGL_RED_SIZE;
366 __eglConfigList[index++] = 5;
367 __eglConfigList[index++] = EGL_GREEN_SIZE;
368 __eglConfigList[index++] = 6;
369 __eglConfigList[index++] = EGL_BLUE_SIZE;
370 __eglConfigList[index++] = 5;
372 case EGL_ATTRIBUTES_PRESET_ARGB8888:
373 __eglConfigList[index++] = EGL_ALPHA_SIZE;
374 __eglConfigList[index++] = 8;
375 __eglConfigList[index++] = EGL_RED_SIZE;
376 __eglConfigList[index++] = 8;
377 __eglConfigList[index++] = EGL_GREEN_SIZE;
378 __eglConfigList[index++] = 8;
379 __eglConfigList[index++] = EGL_BLUE_SIZE;
380 __eglConfigList[index++] = 8;
382 case EGL_ATTRIBUTES_PRESET_DONT_CARE:
385 SysTryReturnResult(NID_GRP, false, E_INVALID_ARG, "Input preset is invalid. ");
388 __eglConfigList[index] = EGL_NONE;
394 _GlPlayerImpl::SetEglAttribute(EGLint key, EGLint value)
398 if (key != EGL_BUFFER_SIZE && key != EGL_RED_SIZE && key != EGL_GREEN_SIZE && key != EGL_BLUE_SIZE && key != EGL_ALPHA_SIZE &&
399 key != EGL_CONFIG_CAVEAT && key != EGL_CONFIG_ID && key != EGL_DEPTH_SIZE && key != EGL_LEVEL && key != EGL_NATIVE_RENDERABLE &&
400 key != EGL_NATIVE_VISUAL_TYPE && key != EGL_SAMPLE_BUFFERS && key != EGL_SAMPLES && key != EGL_STENCIL_SIZE && key != EGL_SURFACE_TYPE &&
401 key != EGL_TRANSPARENT_TYPE && key != EGL_TRANSPARENT_RED_VALUE && key != EGL_TRANSPARENT_GREEN_VALUE && key != EGL_TRANSPARENT_BLUE_VALUE)
403 SysTryReturnResult(NID_GRP, false, E_INVALID_ARG, "Input key is invalid for EGL attributes");
406 while (__eglConfigList[index] != key && __eglConfigList[index] != EGL_NONE)
411 if (__eglConfigList[index] == EGL_NONE)
413 __eglConfigList[index] = key;
414 __eglConfigList[index+2] = EGL_NONE;
416 __eglConfigList[index+1] = value;
422 _GlPlayerImpl::SetIGlRenderer(IGlRenderer* pRenderer)
424 if (pRenderer != null)
431 __pTargetControl->GetBounds(x, y, width, height);
432 pRenderer->SetTargetControlWidth(width);
433 pRenderer->SetTargetControlHeight(height);
435 SysTryReturnResult(NID_GRP, pRenderer->InitializeGl(), E_INVALID_OPERATION, "Fail to InitializeGl() for the given pRenderer. ");
437 if (__pRenderer != null)
439 __pRenderer->TerminateGl();
442 __pRenderer = pRenderer;
448 _GlPlayerImpl::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
450 if (__pTimer == null)
454 __pTimer->Start(__renderTimeInterval);
456 if (Redraw() != E_SUCCESS)
458 SysLog(NID_GRP, "Redraw() in OnTimerExpired() has failed. ");
463 _GlPlayerImpl::GetInstance(GlPlayer& player)
465 return (&player != null) ? player.__pImpl : null;
469 _GlPlayerImpl::GetInstance(const GlPlayer& player)
471 return (&player != null) ? player.__pImpl : null;
474 }}} // Tizen::Graphics::Opengl