Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / FUiEffects_EffectManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17 /**
18  * @file        FUiEffects_EffectManagerImpl.cpp
19  * @brief               This is the source file for the _EffectManagerImpl class
20  */
21
22 #include <FUiEffects_EffectManagerImpl.h>
23
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27
28 #include <FBaseColIList.h>
29 #include <FBaseSysLog.h>
30 #include <FBaseErrors.h>
31 #include <FUiTouchEventInfo.h>
32
33 #include <FUiEffectsEffect.h>
34 #include <FUiEffectsEffectManager.h>
35 #include <FUiEffectsEffectTouchInfo.h>
36 #include <FUiEffectsIEffectEventListener.h>
37 #include <FUiEffectsIEffectResourceProvider.h>
38
39 #include <FUi_CoordinateSystemUtils.h>
40 #include "FUiEffects_EffectImpl.h"
41 #include "FUiEffects_EffectManagerImpl.h"
42 #include <FUiEffects_LoggingProfiler.h>
43 #include <FUiEffects_RuntimeIEffectModelListener.h>
44 #include <FUiEffects_ParserEffectParser.h>
45 #include "FUiEffects_EffectErrorMessages.h"
46
47 using namespace Tizen::Ui::Controls;
48 using namespace Tizen::Base::Runtime;
49 using namespace Tizen::Ui;
50 using namespace Tizen::Base;
51 using namespace Tizen::Graphics;
52 using namespace Tizen::Base::Collection;
53
54 using namespace Tizen::Ui::Effects::_Runtime;
55 using namespace Tizen::Ui::Effects::_Renderer;
56 using namespace Tizen::Ui::Effects::_Parser;
57 using namespace Tizen::Ui::Effects;
58 using namespace Tizen::Ui::Effects::_Utils;
59 using namespace std;
60
61 using namespace Tizen::Ui::Effects::_Parser;
62
63 namespace Tizen { namespace Ui { namespace Effects
64 {
65
66 _EffectManagerImpl* pEffectManagerImpl = null;
67 EffectManager* pEffectManager = null;
68 pthread_once_t once_block = PTHREAD_ONCE_INIT;
69
70 const char* _EffectManagerImpl::__pPathToEffectFile = null;
71
72 Effect*
73 _EffectManagerImpl::CreateEffect(const Tizen::Base::String& filePath)
74 {
75         SetLastResult(E_SUCCESS);
76
77         bool fileExists = false;
78         std::fstream fin;
79
80         std::wstring tempWStr(filePath.GetPointer());
81         std::string filePathStr(tempWStr.begin(), tempWStr.end());
82         fin.open(filePathStr.c_str(), std::ios::in);
83         fileExists = fin.is_open();
84         fin.close();
85
86         SysTryReturn(NID_UI_EFFECT, fileExists, null, E_FILE_NOT_FOUND, _UiEffectError::NO_EFFECT_FILE, filePathStr.c_str());
87
88         __pPathToEffectFile = filePathStr.c_str();
89
90         Effect* pEffect = new (std::nothrow) Effect();
91         SysTryReturn(NID_UI_EFFECT, pEffect != null, null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
92
93         result r = GetLastResult();
94         SysTryCatch(NID_UI_EFFECT, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
95
96         SysLog(NID_UI_EFFECT, "Effect from file '%ls' is loaded successfully", filePath.GetPointer());
97         return pEffect;
98
99 CATCH:
100         delete pEffect;
101         return null;
102 }
103
104 void
105 _EffectManagerImpl::DestroyEffect(Effect& effect)
106 {
107         SetLastResult(E_SUCCESS);
108
109         if (_EffectImpl::__effects.find(&effect) != _EffectImpl::__effects.end())
110         {
111                 SysLog(NID_UI_EFFECT, "The effect [%ls] is destroying now", effect.GetName().GetPointer());
112                 delete &effect;
113                 SysLog(NID_UI_EFFECT, "The effect is destroyed successfully");
114         }
115         else
116         {
117                 SysLog(NID_UI_EFFECT, "The effect was destroyed before");
118         }
119
120         return;
121 }
122
123 _EffectManagerImpl::_EffectManagerImpl(void)
124 {
125         if (EffectRenderer::InitOpenGL())
126         {
127                 SysLog(NID_UI_EFFECT, "Effects. OpenGL initialization is successful!");
128         }
129         else
130         {
131                 SysLogException(NID_UI_EFFECT, E_OPERATION_FAILED, "[E_OPERATION_FAILED] OpenGL initialization failed");
132         }
133 }
134
135 _EffectManagerImpl::~_EffectManagerImpl(void)
136 {
137         // destroy all effects
138         // operator while is here because in Effect class destuctor we erase element from container __effects
139         while (true)
140         {
141                 if (_EffectImpl::__effects.empty())
142                 {
143                         break;
144                 }
145                 delete *(_EffectImpl::__effects.begin());
146         }
147
148         _EffectImpl::__renderControls.clear();
149         _EffectImpl::__controlsSetPerforming.clear();
150
151         EffectRenderer::CloseOpenGL();
152 }
153
154 void
155 _EffectManagerImpl::InitSingleton(void)
156 {
157         SysAssertf(pEffectManagerImpl == null, _UiEffectError::INTERNAL_ERROR);
158         SysAssertf(pEffectManager == null, _UiEffectError::INTERNAL_ERROR);
159
160         std::unique_ptr<_EffectManagerImpl> pEffectManagerImplTemp(new (std::nothrow) _EffectManagerImpl());
161         SysTryReturnVoidResult(NID_UI_EFFECT, pEffectManagerImplTemp.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
162
163         pEffectManager = new (std::nothrow) EffectManager();
164         SysTryReturnVoidResult(NID_UI_EFFECT,  pEffectManager != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
165
166         pEffectManagerImpl = pEffectManagerImplTemp.release();
167 }
168
169 _EffectManagerImpl::ManagerAndImplPair
170 _EffectManagerImpl::GetInstance(void)
171 {
172         SetLastResult(E_SUCCESS);
173
174         if (pEffectManagerImpl == null)
175         {
176                 SysAssertf(pEffectManager == null, _UiEffectError::INTERNAL_ERROR);
177                 pthread_once(&once_block, InitSingleton);
178                 result res = GetLastResult();
179                 SysTryReturn(NID_UI_EFFECT, res == E_SUCCESS, std::make_pair(static_cast<EffectManager*>(null), static_cast<_EffectManagerImpl*>(null)), res, "[%s] Propagating.", GetErrorMessage(res));
180         }
181
182         return std::make_pair(pEffectManager, pEffectManagerImpl);
183 }
184
185 void
186 _EffectManagerImpl::DestroyInstance(void)
187 {
188         SetLastResult(E_SUCCESS);
189         if (pEffectManagerImpl != null)
190         {
191                 SysAssertf(pEffectManager != null, _UiEffectError::INTERNAL_ERROR);
192                 delete pEffectManagerImpl;
193                 delete pEffectManager;
194                 pEffectManagerImpl = null;
195                 pEffectManager = null;
196                 once_block = PTHREAD_ONCE_INIT;
197         }
198         return;
199 }
200
201 } } } //Tizen::Ui::Effects