Allowed Bubble emitter to recover from context loss
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "test-application.h"
19
20 namespace Dali
21 {
22
23
24 TestApplication::TestApplication( size_t surfaceWidth,
25                                   size_t surfaceHeight,
26                                   float  horizontalDpi,
27                                   float  verticalDpi,
28                                   ResourcePolicy::DataRetention policy)
29 : mCore( NULL ),
30   mSurfaceWidth( surfaceWidth ),
31   mSurfaceHeight( surfaceHeight ),
32   mFrame( 0u ),
33   mDpi( horizontalDpi, verticalDpi ),
34   mLastVSyncTime(0u),
35   mDataRetentionPolicy( policy )
36 {
37   Initialize();
38 }
39
40 TestApplication::TestApplication( bool   initialize,
41                                   size_t surfaceWidth,
42                                   size_t surfaceHeight,
43                                   float  horizontalDpi,
44                                   float  verticalDpi,
45                                   ResourcePolicy::DataRetention policy)
46 : mCore( NULL ),
47   mSurfaceWidth( surfaceWidth ),
48   mSurfaceHeight( surfaceHeight ),
49   mFrame( 0u ),
50   mDpi( horizontalDpi, verticalDpi ),
51   mDataRetentionPolicy( policy )
52 {
53   if ( initialize )
54   {
55     Initialize();
56   }
57 }
58
59 void TestApplication::Initialize()
60 {
61   mCore = Dali::Integration::Core::New(
62     mRenderController,
63     mPlatformAbstraction,
64     mGlAbstraction,
65     mGlSyncAbstraction,
66     mGestureManager,
67     mDataRetentionPolicy);
68
69   mCore->ContextCreated();
70   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
71   mCore->SetDpi( mDpi.x, mDpi.y );
72
73   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
74   Dali::Integration::Log::InstallLogFunction(logFunction);
75 }
76
77 TestApplication::~TestApplication()
78 {
79   Dali::Integration::Log::UninstallLogFunction();
80   delete mCore;
81 }
82
83 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
84 {
85   switch(level)
86   {
87     case Dali::Integration::Log::DebugInfo:
88       fprintf(stderr, "INFO: %s", message.c_str());
89       break;
90     case Dali::Integration::Log::DebugWarning:
91       fprintf(stderr, "WARN: %s", message.c_str());
92       break;
93     case Dali::Integration::Log::DebugError:
94       fprintf(stderr, "ERROR: %s", message.c_str());
95       break;
96     default:
97       fprintf(stderr, "DEFAULT: %s", message.c_str());
98       break;
99   }
100 }
101
102 Dali::Integration::Core& TestApplication::GetCore()
103 {
104   return *mCore;
105 }
106
107 TestPlatformAbstraction& TestApplication::GetPlatform()
108 {
109   return mPlatformAbstraction;
110 }
111
112 TestRenderController& TestApplication::GetRenderController()
113 {
114   return mRenderController;
115 }
116
117 TestGlAbstraction& TestApplication::GetGlAbstraction()
118 {
119   return mGlAbstraction;
120 }
121
122 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
123 {
124   return mGlSyncAbstraction;
125 }
126
127 TestGestureManager& TestApplication::GetGestureManager()
128 {
129   return mGestureManager;
130 }
131
132 void TestApplication::ProcessEvent(const Integration::Event& event)
133 {
134   mCore->QueueEvent(event);
135   mCore->ProcessEvents();
136 }
137
138 void TestApplication::SendNotification()
139 {
140   mCore->ProcessEvents();
141 }
142
143 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
144 {
145   mSurfaceWidth = width;
146   mSurfaceHeight = height;
147
148   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
149 }
150
151 void TestApplication::DoUpdate( unsigned int intervalMilliseconds )
152 {
153   unsigned int seconds(0u), microseconds(0u);
154   mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
155   mLastVSyncTime = ( seconds * 1e3 ) + ( microseconds / 1e3 );
156   unsigned int nextVSyncTime = mLastVSyncTime + 16;
157
158   // Update Time values
159   mPlatformAbstraction.IncrementGetTimeResult( intervalMilliseconds );
160
161   float elapsedSeconds = intervalMilliseconds / 1e3f;
162   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus );
163 }
164
165 bool TestApplication::Render( unsigned int intervalMilliseconds  )
166 {
167   DoUpdate( intervalMilliseconds );
168   mCore->Render( mRenderStatus );
169
170   mFrame++;
171
172   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
173 }
174
175 unsigned int TestApplication::GetUpdateStatus()
176 {
177   return mStatus.KeepUpdating();
178 }
179
180 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
181 {
182   DoUpdate( intervalMilliseconds );
183   return mStatus.KeepUpdating();
184 }
185
186 bool TestApplication::RenderOnly( )
187 {
188   // Update Time values
189   mCore->Render( mRenderStatus );
190
191   mFrame++;
192
193   return mRenderStatus.NeedsUpdate();
194 }
195
196 void TestApplication::ResetContext()
197 {
198   mCore->ContextDestroyed();
199   mCore->ContextCreated();
200 }
201
202
203 } // Namespace dali