Merge "Updates to handle context loss and regain" into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-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   mLastVSyncTime(0u),
52   mDataRetentionPolicy( policy )
53 {
54   if ( initialize )
55   {
56     Initialize();
57   }
58 }
59
60 void TestApplication::Initialize()
61 {
62   mCore = Dali::Integration::Core::New(
63     mRenderController,
64     mPlatformAbstraction,
65     mGlAbstraction,
66     mGlSyncAbstraction,
67     mGestureManager,
68     mDataRetentionPolicy);
69
70   mCore->ContextCreated();
71   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
72   mCore->SetDpi( mDpi.x, mDpi.y );
73
74   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
75   Dali::Integration::Log::InstallLogFunction(logFunction);
76 }
77
78 TestApplication::~TestApplication()
79 {
80   Dali::Integration::Log::UninstallLogFunction();
81   delete mCore;
82 }
83
84 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
85 {
86   switch(level)
87   {
88     case Dali::Integration::Log::DebugInfo:
89       fprintf(stderr, "INFO: %s", message.c_str());
90       break;
91     case Dali::Integration::Log::DebugWarning:
92       fprintf(stderr, "WARN: %s", message.c_str());
93       break;
94     case Dali::Integration::Log::DebugError:
95       fprintf(stderr, "ERROR: %s", message.c_str());
96       break;
97     default:
98       fprintf(stderr, "DEFAULT: %s", message.c_str());
99       break;
100   }
101 }
102
103 Dali::Integration::Core& TestApplication::GetCore()
104 {
105   return *mCore;
106 }
107
108 TestPlatformAbstraction& TestApplication::GetPlatform()
109 {
110   return mPlatformAbstraction;
111 }
112
113 TestRenderController& TestApplication::GetRenderController()
114 {
115   return mRenderController;
116 }
117
118 TestGlAbstraction& TestApplication::GetGlAbstraction()
119 {
120   return mGlAbstraction;
121 }
122
123 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
124 {
125   return mGlSyncAbstraction;
126 }
127
128 TestGestureManager& TestApplication::GetGestureManager()
129 {
130   return mGestureManager;
131 }
132
133 void TestApplication::ProcessEvent(const Integration::Event& event)
134 {
135   mCore->QueueEvent(event);
136   mCore->ProcessEvents();
137 }
138
139 void TestApplication::SendNotification()
140 {
141   mCore->ProcessEvents();
142 }
143
144 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
145 {
146   mSurfaceWidth = width;
147   mSurfaceHeight = height;
148
149   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
150 }
151
152 void TestApplication::DoUpdate( unsigned int intervalMilliseconds )
153 {
154   unsigned int seconds(0u), microseconds(0u);
155   mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
156   mLastVSyncTime = ( seconds * 1e3 ) + ( microseconds / 1e3 );
157   unsigned int nextVSyncTime = mLastVSyncTime + 16;
158
159   // Update Time values
160   mPlatformAbstraction.IncrementGetTimeResult( intervalMilliseconds );
161
162   float elapsedSeconds = intervalMilliseconds / 1e3f;
163   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus );
164 }
165
166 bool TestApplication::Render( unsigned int intervalMilliseconds  )
167 {
168   DoUpdate( intervalMilliseconds );
169   mCore->Render( mRenderStatus );
170
171   mFrame++;
172
173   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
174 }
175
176 unsigned int TestApplication::GetUpdateStatus()
177 {
178   return mStatus.KeepUpdating();
179 }
180
181 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
182 {
183   DoUpdate( intervalMilliseconds );
184   return mStatus.KeepUpdating();
185 }
186
187 bool TestApplication::RenderOnly( )
188 {
189   // Update Time values
190   mCore->Render( mRenderStatus );
191
192   mFrame++;
193
194   return mRenderStatus.NeedsUpdate();
195 }
196
197 void TestApplication::ResetContext()
198 {
199   mCore->ContextDestroyed();
200   mCore->ContextCreated();
201 }
202
203
204 } // Namespace dali