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 FUiEffects_RendererEffectRenderer.cpp
20 * @brief This is the source file for the EffectRenderer class
23 #include <FUiEffects_RendererEffectRenderer.h>
26 #include <unique_ptr.h>
28 #include <FBaseSysLog.h>
29 #include <FUi_CoordinateSystemUtils.h>
30 #include <FGrp_BitmapImpl.h>
32 #include "FUiEffects_RendererEffectShader.h"
33 #include <FUiEffects_RuntimePointLight.h>
34 #include <FUiEffects_RuntimeSpotLight.h>
35 #include <FUiEffects_RuntimeDirectionalLight.h>
36 #include "../FUiEffects_EffectErrorMessages.h"
37 #include <FUiEffects_LoggingProfiler.h>
39 using namespace Tizen::Ui::Effects::_Runtime;
40 using namespace Tizen::Ui::Effects::_Renderer::EngineModel;
41 using namespace Tizen::Ui::Effects::_Renderer::Math;
42 using namespace Tizen::Ui::Effects::_Renderer::GraphicsEngine;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Ui;
47 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer
50 EGLDisplay EffectRenderer::__eglDisplay = EGL_NO_DISPLAY;
51 EGLConfig EffectRenderer::__eglConfig = 0;
52 EGLint EffectRenderer::__glMajorVer = 0;
53 EGLint EffectRenderer::__glMinorVer = 0;
54 bool EffectRenderer::__OpenGLInitialized = false;
56 const EGLint RENDERER_EGL_DEFAULT_CONFIG_ATTRIBS[] =
63 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
64 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
68 static const EGLint RENDERER_EGL_DEFAULT_CONTEXT_ATTRIBS[] =
70 EGL_CONTEXT_CLIENT_VERSION, 2,
75 EffectRenderer::InitOpenGL(const EGLint* pConfig)
77 //if already initialized
78 if (__OpenGLInitialized)
80 SysLog(NID_UI_EFFECT, "OpenGL already initialized.");
86 eglBindAPI(EGL_OPENGL_ES_API);
88 __eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
89 SysTryReturn(NID_UI_EFFECT, __eglDisplay != EGL_NO_DISPLAY, false, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Effects. There was an error while performing function eglGetDisplay");
92 SysTryCatchLabel(NID_UI_EFFECT, eglInitialize(__eglDisplay, &__glMajorVer, &__glMinorVer) == EGL_TRUE, , INIT_ERROR, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Effects. There was an error while performing function eglInitialize");
93 //SysLog(NID_UI_EFFECT, "Effects. GL version %d.%d", __glMajorVer, __glMinorVer);
95 SysTryCatchLabel(NID_UI_EFFECT, eglChooseConfig(__eglDisplay, pConfig, &__eglConfig, 1, &nconfigs) == EGL_TRUE, , INIT_ERROR, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Effects. There was an error while performing function eglChooseConfig");
98 SysTryCatchLabel(NID_UI_EFFECT, nconfigs == 1, , INIT_ERROR, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Effects. There is eglChooseConfig matching configurations count error (OpenGL)");
101 __OpenGLInitialized = CheckEglNoErrors();
103 SysTryReturn(NID_UI_EFFECT, __OpenGLInitialized, false, E_OPERATION_FAILED, "[E_OPERATION_FAILED] There are OpenGL errors during initialization");
105 SysLog(NID_UI_EFFECT, "OpenGL initialized successfully.");
110 __OpenGLInitialized = false;
111 eglTerminate(__eglDisplay);
112 __eglDisplay = EGL_NO_DISPLAY;
113 SysLogException(NID_UI_EFFECT, E_OPERATION_FAILED, "[E_OPERATION_FAILED] OpenGL errors were appeared during initialization");
119 EffectRenderer::CloseOpenGL(void)
121 SysLog(NID_UI_EFFECT, "Effects. OpenGL is closing...");
123 //if already initialized
124 if (!__OpenGLInitialized)
126 SysLog(NID_UI_EFFECT, "OpenGL not initialized.");
131 if (__eglDisplay != EGL_NO_DISPLAY)
133 eglTerminate(__eglDisplay);
134 __eglDisplay = EGL_NO_DISPLAY;
137 __OpenGLInitialized = false;
139 SysLog(NID_UI_EFFECT, "OpenGL closed!");
145 EffectRenderer::CheckOpenGlesInitialized(void)
147 return __OpenGLInitialized;
151 EffectRenderer::CreateSurface(EGLNativeWindowType wnd)
153 EGLSurface newsurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, wnd, 0);
157 SysTryReturn(NID_UI_EFFECT, newsurface != EGL_NO_SURFACE && CheckEglNoErrors(), EGL_NO_SURFACE, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Effects. There was an error while performing function eglCreateWindowSurface (EGL_NO_SURFACE)");
164 EffectRenderer::DeleteSurface(EGLSurface surface)
166 SysTryReturnVoidResult(NID_UI_EFFECT, surface != EGL_NO_SURFACE, E_OPERATION_FAILED, "DeleteSurface EGL_NO_SURFACE");
168 eglDestroySurface(__eglDisplay, surface);
174 EffectRenderer::CreateContext(EGLContext shareContext)
176 EGLContext newContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, RENDERER_EGL_DEFAULT_CONTEXT_ATTRIBS);
178 SysTryReturn(NID_UI_EFFECT, newContext != EGL_NO_CONTEXT && CheckEglNoErrors(), EGL_NO_CONTEXT, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Effects. There was an error while performing function eglCreateContext (EGL_NO_CONTEXT)");
185 EffectRenderer::DeleteContext(EGLContext context)
187 //SysTryLog(NID_UI_EFFECT, context != EGL_NO_CONTEXT, "DeleteContext EGL_NO_CONTEXT");
189 eglDestroyContext(__eglDisplay, context);
194 EffectRenderer::EffectRenderer(void)
195 : __previousContext(EGL_NO_CONTEXT)
196 , __previousContextSaved(false)
198 , __perspectiveProjection(false)
199 , __renderDataScene(null)
200 , __pRenderDataSurfaceCollection(null)
201 , __dimensionPreviousControl()
203 result r = GetLastResult();
204 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
206 surface = EGL_NO_SURFACE;
207 __context = CreateContext();
209 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
211 __renderer = RenderPtr(new (std::nothrow) Render);
213 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
214 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__renderer) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
216 __root = GroupPtr(new (std::nothrow) Group);
218 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
219 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__root) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
221 __camera = CameraPtr(new (std::nothrow) Camera);
223 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
224 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__camera) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
226 __camera->SetWorld(Math::Matrix4f().Identity());
228 StringShaderPropertyPtr vertex(new (std::nothrow) StringShaderProperty(ShaderProperty::ShaderType::VERTEX, EFFECT_VERTEX_SHADER_STR));
229 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(vertex) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
231 StringShaderPropertyPtr fragment(new (std::nothrow) StringShaderProperty(ShaderProperty::ShaderType::FRAGMENT, EFFECT_DUMMY_FRAGMENT_SHADER_STR));
232 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(fragment) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
234 __program = ShaderHolderProgramPropertyPtr(new (std::nothrow) ShaderHolderProgramProperty(vertex, fragment));
236 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
237 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__program) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
239 __renderer->SetDefaultDepthTestState(true);
242 EffectRenderer::~EffectRenderer(void)
245 DeleteContext(__context);
249 EffectRenderer::MakeCurrent(void)
251 SysAssertf(__eglDisplay != EGL_NO_DISPLAY, _UiEffectError::INTERNAL_ERROR);
252 SysAssertf(surface != EGL_NO_SURFACE, _UiEffectError::INTERNAL_ERROR);
254 if (!__previousContextSaved)
256 __previousContext = eglGetCurrentContext();
257 __previousContextSaved = true;
260 eglMakeCurrent(__eglDisplay, surface, surface, __context);
266 EffectRenderer::RestorePreviousContext(void)
268 if (__previousContextSaved && __eglDisplay != EGL_NO_DISPLAY)
270 if (__previousContext != EGL_NO_CONTEXT && surface != EGL_NO_SURFACE)
272 eglMakeCurrent(__eglDisplay, surface, surface, __previousContext);
276 eglMakeCurrent(__eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
280 __previousContextSaved = false;
285 MemoryTexture2DPropertyPtr
286 EffectRenderer::FindTexture(long bitmapID)
288 TexturesMap::const_iterator it = __textures.find(bitmapID);
290 if (it != __textures.end())
296 // create empty placeholder for further texture upload
297 MemoryTexture2DPropertyPtr tex = MemoryTexture2DPropertyPtr(new (std::nothrow) MemoryTexture2DProperty);
299 result r = GetLastResult();
300 SysTryReturn(NID_UI_EFFECT, r == E_SUCCESS, tex, r, "[%s] Propagating.", GetErrorMessage(r));
301 SysTryReturn(NID_UI_EFFECT, System::GetImpl(tex) != null, tex, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
303 __textures.insert(TexturesMap::value_type(bitmapID, tex));
308 EffectRenderer::InitializeContext(void) const
310 glClearColor(0,0,0,1);
311 glDisable(GL_CULL_FACE);
317 EffectRenderer::Construct(RenderDataScene &renderDataCollection)
319 __renderDataScene = &renderDataCollection;
320 __pRenderDataSurfaceCollection = &renderDataCollection.GetRenderDataSurfaceCollection();
322 Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection::size_type objcount = __pRenderDataSurfaceCollection->size();
324 __root->Clear();// no deletion of children? check destructors
325 __root->AddChild(__camera);
329 result r = GetLastResult();
330 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
332 __objects.resize(objcount);
333 for (RenderDataSurfaceCollection::size_type i = 0; i < objcount; ++i)
335 __objects[i] = RendererObjectPtr(new (std::nothrow) RendererObject);
337 result r = GetLastResult();
338 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
339 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__objects[i]) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
342 for (RenderDataSurfaceCollection::size_type i = 0; i < objcount; ++i)
344 InitRObjectFromData(i, *(*__pRenderDataSurfaceCollection)[i]);
351 EffectRenderer::ConstructLighting()
353 SysAssertf(__renderDataScene != null, _UiEffectError::INTERNAL_ERROR);
355 __lightingParameters.SetAmbientColor(__renderDataScene->GetLightAmbientColour());
356 __lightingParameters.SetAmbientIntensity(__renderDataScene->GetLightIntensity());
357 __lightingParameters.SetAttenuation(__renderDataScene->GetLightAttenuation());
359 if (__renderDataScene->LockUnitsLightContainer())
363 switch (__renderDataScene->GetTypeOfCurrentUnitLight())
365 case TYPE_UNIT_LIGHT_POINT:
367 const _Runtime::PointLight* pModelPointLight = __renderDataScene->GetCurrentUnitLight<_Runtime::PointLight>();
368 if (pModelPointLight != null)
370 PointLightPtr pointLight = PointLightPtr(new (std::nothrow) PointLight(__lightingParameters.GetAttenuation()));
371 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(pointLight) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
372 __pointLights[pModelPointLight->GetName()] = pointLight;
374 pointLight->SetPosition(pModelPointLight->GetPosition());
375 pointLight->SetColor(pModelPointLight->GetColour());
376 pointLight->SetIntensity(pModelPointLight->GetIntensity());
377 pointLight->SetEnabled(pModelPointLight->GetEnabled());
381 case TYPE_UNIT_LIGHT_SPOT:
383 const _Runtime::SpotLight* pModelSpotLight = __renderDataScene->GetCurrentUnitLight<_Runtime::SpotLight>();
384 if (pModelSpotLight != null)
386 SpotLightPtr spotLight = SpotLightPtr(new (std::nothrow) SpotLight(__lightingParameters.GetAttenuation()));
387 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(spotLight) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
388 __spotLights[pModelSpotLight->GetName()] = spotLight;
390 spotLight->SetPosition(pModelSpotLight->GetPosition());
391 spotLight->SetTarget(pModelSpotLight->GetTarget());
392 spotLight->SetOpeningAngle(pModelSpotLight->GetAngleOpening());
393 spotLight->SetFadeOutAngle(pModelSpotLight->GetAngleFadeOut());
394 spotLight->SetColor(pModelSpotLight->GetColour());
395 spotLight->SetIntensity(pModelSpotLight->GetIntensity());
396 spotLight->SetEnabled(pModelSpotLight->GetEnabled());
400 case TYPE_UNIT_LIGHT_DIRECTIONAL:
402 const _Runtime::DirectionalLight* pModelDirectionalLight = __renderDataScene->GetCurrentUnitLight<_Runtime::DirectionalLight>();
403 if (pModelDirectionalLight != null)
405 DirectionalLightPtr directionalLight = DirectionalLightPtr(new (std::nothrow) DirectionalLight);
406 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(directionalLight) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
407 __directionalLights[pModelDirectionalLight->GetName()] = directionalLight;
409 directionalLight->SetDirection(pModelDirectionalLight->GetDirection());
410 directionalLight->SetColor(pModelDirectionalLight->GetColour());
411 directionalLight->SetIntensity(pModelDirectionalLight->GetIntensity());
412 directionalLight->SetEnabled(pModelDirectionalLight->GetEnabled());
420 while (__renderDataScene->GoToNextUnitLight());
429 EffectRenderer::UpdateLighting(void)
431 SysAssertf(__renderDataScene != null, _UiEffectError::INTERNAL_ERROR);
433 bool updated = false;
434 bool needShaderCompilation = false;
436 if (__renderDataScene->isSomethingChanged)
440 if (__renderDataScene->isAmbientColourChanged)
442 __lightingParameters.SetAmbientColor(__renderDataScene->GetLightAmbientColour());
443 __renderDataScene->isAmbientColourChanged = false;
446 if (__renderDataScene->isIntensityChanged)
448 __lightingParameters.SetAmbientIntensity(__renderDataScene->GetLightIntensity());
449 __renderDataScene->isIntensityChanged = false;
452 if (__renderDataScene->isAttenuationChanged)
454 __lightingParameters.SetAttenuation(__renderDataScene->GetLightAttenuation());
456 for (PointLightsList::iterator it = __pointLights.begin(); it != __pointLights.end(); ++it)
458 it->second->UpdateAttenuation();
461 for (SpotLightsList::iterator it = __spotLights.begin(); it != __spotLights.end(); ++it)
463 it->second->UpdateAttenuation();
466 __renderDataScene->isAttenuationChanged = false;
468 __renderDataScene->isSomethingChanged = false;
471 if (__renderDataScene->LockUnitsLightContainer())
475 const _Runtime::UnitLight *pLight = __renderDataScene->GetCurrentUnitLight<_Runtime::UnitLight>();
476 updated = updated || pLight->isSomethingChanged;
478 if (pLight->isSomethingChanged)
480 string nameUnitLight = pLight->GetName();
482 switch (pLight->GetType())
484 case TYPE_UNIT_LIGHT_POINT:
486 const _Runtime::PointLight* pModelPointLight = static_cast<const _Runtime::PointLight*>(pLight);
487 PointLight *pPointLight = System::GetImpl(__pointLights[nameUnitLight]);
489 if (pModelPointLight->isPositionChanged)
491 pPointLight->SetPosition(pModelPointLight->GetPosition());
494 if (pModelPointLight->isColourChanged)
496 pPointLight->SetColor(pModelPointLight->GetColour());
499 if (pModelPointLight->isIntensityChanged)
501 pPointLight->SetIntensity(pModelPointLight->GetIntensity());
504 if (pModelPointLight->isEnabledChanged)
506 needShaderCompilation = true;
507 pPointLight->SetEnabled(pModelPointLight->GetEnabled());
512 case TYPE_UNIT_LIGHT_SPOT:
514 const _Runtime::SpotLight* pModelSpotLight = static_cast<const _Runtime::SpotLight*>(pLight);
515 SpotLight *pSpotLight = System::GetImpl(__spotLights[nameUnitLight]);
517 if (pModelSpotLight->isPositionChanged)
519 pSpotLight->SetPosition(pModelSpotLight->GetPosition());
522 if (pModelSpotLight->isTargetChanged)
524 pSpotLight->SetTarget(pModelSpotLight->GetTarget());
527 if (pModelSpotLight->isAngleOpeningChanged)
529 pSpotLight->SetOpeningAngle(pModelSpotLight->GetAngleOpening());
532 if (pModelSpotLight->isAngleFadeOutChanged)
534 pSpotLight->SetFadeOutAngle(pModelSpotLight->GetAngleFadeOut());
537 if (pModelSpotLight->isColourChanged)
539 pSpotLight->SetColor(pModelSpotLight->GetColour());
542 if (pModelSpotLight->isIntensityChanged)
544 pSpotLight->SetIntensity(pModelSpotLight->GetIntensity());
547 if (pModelSpotLight->isEnabledChanged)
549 needShaderCompilation = true;
550 pSpotLight->SetEnabled(pModelSpotLight->GetEnabled());
554 case TYPE_UNIT_LIGHT_DIRECTIONAL:
556 const _Runtime::DirectionalLight* pModelDirectionalLight = static_cast<const _Runtime::DirectionalLight*>(pLight);
557 DirectionalLight *pDirectionalLight = System::GetImpl(__directionalLights[nameUnitLight]);
559 if (pModelDirectionalLight->isDirectionChanged)
561 pDirectionalLight->SetDirection(pModelDirectionalLight->GetDirection());
564 if (pModelDirectionalLight->isColourChanged)
566 pDirectionalLight->SetColor(pModelDirectionalLight->GetColour());
569 if (pModelDirectionalLight->isIntensityChanged)
571 pDirectionalLight->SetIntensity(pModelDirectionalLight->GetIntensity());
574 if (pModelDirectionalLight->isEnabledChanged)
576 needShaderCompilation = true;
577 pDirectionalLight->SetEnabled(pModelDirectionalLight->GetEnabled());
585 __renderDataScene->ResetSignsCurrentUnitLight();
589 while (__renderDataScene->GoToNextUnitLight());
593 if (needShaderCompilation)
598 std::vector<RendererObjectPtr>::size_type n = __objects.size();
599 for (std::vector<RendererObjectPtr>::size_type i = 0; i < n; ++i)
601 __objects[i]->GetMaterial()->TouchDrawables();
602 __objects[i]->InvalidateCache();
611 EffectRenderer::CreateShader()
613 string __fragmentSource = EFFECT_FRAGMENT_SHADER_STR;
616 if (__pointLights.size() > 0)
622 string::size_type defpos = __fragmentSource.find(POINTLIGHT_DEFPOS_STR);
623 string::size_type srcpos = __fragmentSource.find(POINTLIGHT_SRCPOS_STR);
625 SysAssert(defpos != string::npos);
626 SysAssert(srcpos != string::npos);
630 for (PointLightsList::const_iterator it = __pointLights.begin(); it != __pointLights.end(); ++it)
632 if (!it->second->GetEnabled())
637 def_entry = POINTLIGHT_DEF_STR;
638 sprintf(buffer, "%d", i);
639 replace(def_entry, "%n", buffer);
640 __fragmentSource.insert(defpos, def_entry);
641 defpos += def_entry.length();
642 srcpos += def_entry.length();
644 src_entry = it->second->IsBlack()? POINTLIGHT_NOATTSRC_STR : POINTLIGHT_ATTSRC_STR;
645 sprintf(buffer, "%d", i);
646 replace(src_entry, "%n", buffer);
647 __fragmentSource.insert(srcpos, src_entry);
648 srcpos += src_entry.length();
654 if (__spotLights.size() > 0)
660 string::size_type defpos = __fragmentSource.find(SPOTLIGHT_DEFPOS_STR);
661 string::size_type srcpos = __fragmentSource.find(SPOTLIGHT_SRCPOS_STR);
663 SysAssert(defpos != string::npos);
664 SysAssert(srcpos != string::npos);
668 for (SpotLightsList::const_iterator it = __spotLights.begin(); it != __spotLights.end(); ++it)
670 if (!it->second->GetEnabled())
675 def_entry = SPOTLIGHT_DEF_STR;
676 sprintf(buffer, "%d", i);
677 replace(def_entry, "%n", buffer);
678 __fragmentSource.insert(defpos, def_entry);
679 defpos += def_entry.length();
680 srcpos += def_entry.length();
682 src_entry = it->second->IsBlack()? SPOTLIGHT_NOATTSRC_STR : SPOTLIGHT_ATTSRC_STR;
683 sprintf(buffer, "%d", i);
684 replace(src_entry, "%n", buffer);
685 __fragmentSource.insert(srcpos, src_entry);
686 srcpos += src_entry.length();
692 if (__directionalLights.size() > 0)
698 string::size_type defpos = __fragmentSource.find(DIRECTIOALLIGHT_DEFPOS_STR);
699 string::size_type srcpos = __fragmentSource.find(DIRECTIOALLIGHT_SRCPOS_STR);
701 SysAssert(defpos != string::npos);
702 SysAssert(srcpos != string::npos);
706 for (DirectionalLightsList::const_iterator it = __directionalLights.begin(); it != __directionalLights.end(); ++it)
708 if (!it->second->GetEnabled())
713 def_entry = DIRECTIOALLIGHT_DEF_STR;
714 sprintf(buffer, "%d", i);
715 replace(def_entry, "%n", buffer);
716 __fragmentSource.insert(defpos, def_entry);
717 defpos += def_entry.length();
718 srcpos += def_entry.length();
720 src_entry = DIRECTIOALLIGHT_SRC_STR;
721 sprintf(buffer, "%d", i);
722 replace(src_entry, "%n", buffer);
723 __fragmentSource.insert(srcpos, src_entry);
724 srcpos += src_entry.length();
730 __program->GetShader(EngineModel::ShaderProperty::ShaderType::FRAGMENT).staticCast<StringShaderProperty>()->SetSource(__fragmentSource);
731 if (System::GetImpl(__program->GetCache()) != null)
733 __program->GetCache()->Invalidate();
736 PRINT_DEBUG("Shader ", __fragmentSource);
742 EffectRenderer::InitRObjectFromData(RenderDataSurfaceCollection::size_type index, RenderDataSurface &rdata)
744 SysAssertf(index < __objects.size(), "Effects. Renderer: index < __objects.size()");
746 __objects[index]->opacity->Set(1.0f - rdata.transparency);
747 __objects[index]->alphablend->Enable(rdata.transparency > 0.0f);
749 __objects[index]->GetMat()->AddProperty("Lib Effects Rendering Program", __program);
750 __objects[index]->GetMat()->AddProperty(MAIN_TEX_STR, FindTexture(rdata.bitmapId));
751 __objects[index]->GetMat()->AddProperty(LIGHTING_AMBIENTCOLORI_STR, __lightingParameters.GetAmbientColorIHolder());
756 for(PointLightsList::const_iterator it = __pointLights.begin(); it != __pointLights.end(); ++it)
758 sprintf(buffer, "%d", i);
760 __objects[index]->GetMat()->AddProperty((string(POINTLIGHT_STR) + buffer + POINTLIGHT_POSITION_STR).c_str(), (*it).second->GetPositionHolder());
761 __objects[index]->GetMat()->AddProperty((string(POINTLIGHT_STR) + buffer + POINTLIGHT_COLORI_STR).c_str(), (*it).second->GetColorIHolder());
762 __objects[index]->GetMat()->AddProperty((string(POINTLIGHT_STR) + buffer + POINTLIGHT_RK_STR).c_str(), (*it).second->GetRkHolder());
768 for (DirectionalLightsList::const_iterator it = __directionalLights.begin(); it != __directionalLights.end(); ++it)
770 sprintf(buffer, "%d", i);
772 __objects[index]->GetMat()->AddProperty((string(DIRECTIOALLIGHT_STR) + buffer + DIRECTIOALLIGHT_DIRECTION_STR).c_str(), (*it).second->GetDirectionHolder());
773 __objects[index]->GetMat()->AddProperty((string(DIRECTIOALLIGHT_STR) + buffer + DIRECTIOALLIGHTLIGHT_COLORI_STR).c_str(), (*it).second->GetColorIHolder());
779 for (SpotLightsList::const_iterator it = __spotLights.begin(); it != __spotLights.end(); ++it)
781 sprintf(buffer, "%d", i);
783 __objects[index]->GetMat()->AddProperty((string(SPOTLIGHT_STR) + buffer + SPOTLIGHT_POSITION_STR).c_str(), (*it).second->GetPositionHolder());
784 __objects[index]->GetMat()->AddProperty((string(SPOTLIGHT_STR) + buffer + SPOTLIGHT_DIRECTION_STR).c_str(), (*it).second->GetDirectionHolder());
785 __objects[index]->GetMat()->AddProperty((string(SPOTLIGHT_STR) + buffer + SPOTLIGHT_K1_STR).c_str(), (*it).second->GetK1Holder());
786 __objects[index]->GetMat()->AddProperty((string(SPOTLIGHT_STR) + buffer + SPOTLIGHT_K2_STR).c_str(), (*it).second->GetK2Holder());
787 __objects[index]->GetMat()->AddProperty((string(SPOTLIGHT_STR) + buffer + SPOTLIGHT_COLORI_STR).c_str(), (*it).second->GetColorIHolder());
788 __objects[index]->GetMat()->AddProperty((string(SPOTLIGHT_STR) + buffer + SPOTLIGHT_RK_STR).c_str(), (*it).second->GetRkHolder());
793 RendererGeometryPtr geom(new (std::nothrow) RendererGeometry);
794 result r = GetLastResult();
795 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
796 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(geom) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
798 __objects[index]->setGeometry(geom);
800 __objects[index]->GetGeom()->SetVertData(rdata.vnt);
801 __objects[index]->GetGeom()->SetIndices(rdata.indices);
803 __objects[index]->modelMtr->Set(Math::Matrix4f(rdata.modelMtr[0], rdata.modelMtr[4], rdata.modelMtr[8], rdata.modelMtr[12],
804 rdata.modelMtr[1], rdata.modelMtr[5], rdata.modelMtr[9], rdata.modelMtr[13],
805 rdata.modelMtr[2], rdata.modelMtr[6], rdata.modelMtr[10], rdata.modelMtr[14],
806 rdata.modelMtr[3], rdata.modelMtr[7], rdata.modelMtr[11], rdata.modelMtr[15]));
808 Math::Matrix4f tmpNormalMtr(Math::Matrix4f::GetIdentity());
809 __objects[index]->normalMtr->Set(tmpNormalMtr.SetRotation(__objects[index]->modelMtr->Get().GetRotation().Inverse())); // ".transpose()" is omitted due to "column order" matrix representation in GLSL
811 __root->AddChild(__objects[index]);
817 EffectRenderer::Update(void)
820 std::unique_ptr<int> x;
822 SysAssertf(__renderDataScene != null, _UiEffectError::INTERNAL_ERROR);
823 SysAssertf(__pRenderDataSurfaceCollection != null, _UiEffectError::INTERNAL_ERROR);
824 SysAssertf(__objects.size() == __pRenderDataSurfaceCollection->size(), _UiEffectError::INTERNAL_ERROR);
826 Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection::size_type objcount = __pRenderDataSurfaceCollection->size();
828 for (RenderDataSurfaceCollection::size_type i = 0; i < objcount; ++i)
830 UpdateRObject(i, *(*__pRenderDataSurfaceCollection)[i]);
839 EffectRenderer::UpdateRObject(RenderDataSurfaceCollection::size_type index, RenderDataSurface &rdata)
841 if (rdata.vertexDataChanged || rdata.indicesChanged)
843 RendererGeometryPtr geometry = __objects[index]->GetGeom();
845 if (rdata.vertexDataChanged)
847 geometry->InvalidateVertexData();
848 rdata.vertexDataChanged = false;
851 if (rdata.indicesChanged)
853 geometry->InvalidateIndices();
854 rdata.indicesChanged = false;
857 geometry->TouchDrawables();
860 if (rdata.modelMtrChanged)
862 __objects[index]->modelMtr->Set(Math::Matrix4f(rdata.modelMtr[0], rdata.modelMtr[4], rdata.modelMtr[8], rdata.modelMtr[12],
863 rdata.modelMtr[1], rdata.modelMtr[5], rdata.modelMtr[9], rdata.modelMtr[13],
864 rdata.modelMtr[2], rdata.modelMtr[6], rdata.modelMtr[10], rdata.modelMtr[14],
865 rdata.modelMtr[3], rdata.modelMtr[7], rdata.modelMtr[11], rdata.modelMtr[15]));
867 Math::Matrix4f tmpNormalMtr(Math::Matrix4f::GetIdentity());
869 __objects[index]->normalMtr->Set(tmpNormalMtr.SetRotation(__objects[index]->modelMtr->Get().GetRotation().Inverse())); // ".transpose()" is omitted due to "column order" matrix representation in GLSL
870 __objects[index]->InvalidateCache();
871 rdata.modelMtrChanged = false;
874 if (rdata.bitmapIdChanged || rdata.transparencyChanged)
876 PropertyHolderMaterialPtr material = __objects[index]->GetMat();
878 if (rdata.bitmapIdChanged)
880 material->AddProperty(MAIN_TEX_STR, FindTexture(rdata.bitmapId));
881 rdata.bitmapIdChanged = false;
884 if (rdata.transparencyChanged)
886 __objects[index]->opacity->Set(1.0f - rdata.transparency);
887 __objects[index]->alphablend->Enable(rdata.transparency > 0.0f);
888 rdata.transparencyChanged = false;
891 material->TouchDrawables();
898 EffectRenderer::Draw(void)
900 SysAssertf(__eglDisplay != EGL_NO_DISPLAY, _UiEffectError::INTERNAL_ERROR);
901 SysAssertf(surface != EGL_NO_SURFACE, _UiEffectError::INTERNAL_ERROR);
902 SysAssertf(__context != EGL_NO_CONTEXT, _UiEffectError::INTERNAL_ERROR);
906 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
907 __renderer->Draw(*__root);
908 eglSwapBuffers(__eglDisplay, surface);
910 RestorePreviousContext();
916 EffectRenderer::SetScreen(float screenLeft, float screenRight, float screenBottom, float screenTop, int displayWidth, int displayHeight)
918 SysAssertf(screenRight > screenLeft, _UiEffectError::INTERNAL_ERROR);
919 SysAssertf(screenTop < screenBottom, _UiEffectError::INTERNAL_ERROR);
920 SysAssertf(displayWidth > 0, _UiEffectError::INTERNAL_ERROR);
921 SysAssertf(displayHeight > 0, _UiEffectError::INTERNAL_ERROR);
923 __viewport.screenLeft = screenLeft;
924 __viewport.screenRight = screenRight;
925 __viewport.screenTop = screenTop;
926 __viewport.screenBottom = screenBottom;
927 __viewport.displayWidth = displayWidth;
928 __viewport.displayHeight = displayHeight;
930 __camera->SetViewport(Vector2f(0,0), Vector2f(displayWidth, displayHeight));
936 EffectRenderer::TrackScreenSizeChanges(const Control& control)
938 if (__dimensionPreviousControl != control.GetSize())
946 EffectRenderer::SetScreen(const Control& control)
948 Dimension logicalScreenSizeCurrent = control.GetSize(); // not real but logical size of control
949 Dimension realScreenSize = _CoordinateSystemUtils::Transform(logicalScreenSizeCurrent); // real size of control
950 SetScreen(0, logicalScreenSizeCurrent.width, logicalScreenSizeCurrent.height, 0, realScreenSize.width, realScreenSize.height);
951 __dimensionPreviousControl = logicalScreenSizeCurrent;
956 EffectRenderer::SetWorld(float worldLeft, float worldRight, float worldBottom, float worldTop, float worldZ, float worldNearDist, float worldFarDist, float FOV_Y_deg, bool perspectiveProjection)
958 SysAssertf(fabs(worldLeft - worldRight) > std::numeric_limits<float>::epsilon(), _UiEffectError::INTERNAL_ERROR);
959 SysAssertf(fabs(worldTop - worldBottom) > std::numeric_limits<float>::epsilon(), _UiEffectError::INTERNAL_ERROR);
960 SysAssertf(FOV_Y_deg < 180.0f && FOV_Y_deg > 0.0f, _UiEffectError::INTERNAL_ERROR);
961 SysAssertf(worldNearDist > 0.0f && worldFarDist > 0.0f && worldNearDist < worldFarDist, _UiEffectError::INTERNAL_ERROR);
963 __viewport.worldLeft = worldLeft;
964 __viewport.worldRight = worldRight;
965 __viewport.worldTop = worldTop;
966 __viewport.worldBottom = worldBottom;
967 __viewport.worldZ = worldZ;
968 __viewport.worldNearDist = worldNearDist;
969 __viewport.worldFarDist = worldFarDist;
971 __FOV_Y_deg = FOV_Y_deg;
972 __perspectiveProjection = perspectiveProjection;
974 Math::Matrix4f tmpmat;
976 if (__perspectiveProjection)
978 float cameraHeight = (__viewport.worldTop - __viewport.worldBottom) / 2 / tan(Math::F_Deg_2_Rad(__FOV_Y_deg)/2.0f) + __viewport.worldZ;
980 tmpmat = Math::Matrix4f::CreatePerspectiveFovRH(Math::F_Deg_2_Rad(__FOV_Y_deg), (__viewport.worldRight - __viewport.worldLeft) / (__viewport.worldTop - __viewport.worldBottom), __viewport.worldNearDist, __viewport.worldFarDist);
981 tmpmat.data[1].data[1] = -tmpmat.data[1].data[1];
982 __camera->SetProj(tmpmat);
984 tmpmat = Math::Matrix4f::GetIdentity();
985 tmpmat.SetTranslation((__viewport.worldRight + __viewport.worldLeft) / 2, (__viewport.worldTop + __viewport.worldBottom) / 2, cameraHeight);
986 __camera->SetWorld(tmpmat);
990 tmpmat = Math::Matrix4f::GetIdentity();
991 __camera->SetWorld(tmpmat);
993 tmpmat = Math::Matrix4f::CreateOrthoRH(__viewport.worldLeft, __viewport.worldRight, __viewport.worldBottom, __viewport.worldTop, -__viewport.worldFarDist, __viewport.worldFarDist);
994 __camera->SetProj(tmpmat);
1000 Tizen::Ui::Effects::_Utils::Vec3f
1001 EffectRenderer::TransformScreenToWorld(Tizen::Ui::Effects::_Utils::Vec2f p) const
1003 return __viewport.TransformScreenToWorld(p);
1007 EffectRenderer::SetBitmap(long bitmapID, Tizen::Graphics::Bitmap &bitmap)
1009 MemoryTexture2DPropertyPtr tex = FindTexture(bitmapID);
1010 Tizen::Graphics::_BitmapImpl* pBitmapImpl = Tizen::Graphics::_BitmapImpl::GetInstance(bitmap);
1011 BufferInfo bufferInfo;
1013 SysTryReturn(NID_UI_EFFECT, pBitmapImpl != null, false, E_OPERATION_FAILED, "[E_OPERATION_FAILED] _BitmapImpl::GetInstance has returned null value");
1015 PROFILER_EXPR(pBitmapImpl->LockFast(bufferInfo);, PROFLOG_Lock);
1017 if (bufferInfo.pPixels == null || bufferInfo.width <= 0 || bufferInfo.height <= 0)
1023 PROFILER_EXPR(tex->SetData((void*) bufferInfo.pPixels, bufferInfo.width, bufferInfo.height);, PROFLOG_Setdata);
1024 PROFILER_EXPR(pBitmapImpl->UnlockFast();, PROFLOG_Unlock);
1030 EffectRenderer::SetBitmap(long bitmapID, const void* ptr, size_t width, size_t height)
1032 //ptr = null is OK for texture memory allocation
1033 FindTexture(bitmapID)->SetData(ptr, width, height);
1038 void EffectRenderer::BuildCache()
1041 __renderer->InternalCollectInfo(*__root);
1042 RestorePreviousContext();
1045 void EffectRenderer::ReleaseCache()
1047 if (surface != EGL_NO_SURFACE)
1051 RestorePreviousContext();
1055 }}}} //Tizen::Ui::Effects::_Renderer