Modified spec file to add support for tracing.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2017 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   // We always need the first update!
63   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
64
65   mCore = Dali::Integration::Core::New( mRenderController,
66                                         mPlatformAbstraction,
67                                         mGlAbstraction,
68                                         mGlSyncAbstraction,
69                                         mGestureManager,
70                                         mDataRetentionPolicy,
71                                         Integration::RenderToFrameBuffer::FALSE,
72                                         Integration::DepthBufferAvailable::TRUE,
73                                         Integration::StencilBufferAvailable::TRUE );
74
75   mCore->ContextCreated();
76   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
77   mCore->SetDpi( mDpi.x, mDpi.y );
78
79   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
80   Dali::Integration::Log::InstallLogFunction(logFunction);
81
82   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
83   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
84
85   Dali::Integration::Trace::LogContext( true, "Test" );
86
87   mCore->SceneCreated();
88 }
89
90 TestApplication::~TestApplication()
91 {
92   Dali::Integration::Log::UninstallLogFunction();
93   delete mCore;
94 }
95
96 void TestApplication::LogContext( bool start, const char* tag )
97 {
98   if( start )
99   {
100     fprintf(stderr, "INFO: Trace Start: %s", tag);
101   }
102   else
103   {
104     fprintf(stderr, "INFO: Trace End: %s", tag);
105   }
106 }
107
108 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
109 {
110   switch(level)
111   {
112     case Dali::Integration::Log::DebugInfo:
113       fprintf(stderr, "INFO: %s", message.c_str());
114       break;
115     case Dali::Integration::Log::DebugWarning:
116       fprintf(stderr, "WARN: %s", message.c_str());
117       break;
118     case Dali::Integration::Log::DebugError:
119       fprintf(stderr, "ERROR: %s", message.c_str());
120       break;
121     default:
122       fprintf(stderr, "DEFAULT: %s", message.c_str());
123       break;
124   }
125 }
126
127 Dali::Integration::Core& TestApplication::GetCore()
128 {
129   return *mCore;
130 }
131
132 TestPlatformAbstraction& TestApplication::GetPlatform()
133 {
134   return mPlatformAbstraction;
135 }
136
137 TestRenderController& TestApplication::GetRenderController()
138 {
139   return mRenderController;
140 }
141
142 TestGlAbstraction& TestApplication::GetGlAbstraction()
143 {
144   return mGlAbstraction;
145 }
146
147 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
148 {
149   return mGlSyncAbstraction;
150 }
151
152 TestGestureManager& TestApplication::GetGestureManager()
153 {
154   return mGestureManager;
155 }
156
157 void TestApplication::ProcessEvent(const Integration::Event& event)
158 {
159   mCore->QueueEvent(event);
160   mCore->ProcessEvents();
161 }
162
163 void TestApplication::SendNotification()
164 {
165   mCore->ProcessEvents();
166 }
167
168 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
169 {
170   mSurfaceWidth = width;
171   mSurfaceHeight = height;
172
173   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
174 }
175
176 void TestApplication::SetTopMargin( unsigned int margin )
177 {
178   mCore->SetTopMargin( margin );
179 }
180
181 void TestApplication::DoUpdate( unsigned int intervalMilliseconds, const char* location )
182 {
183   if( GetUpdateStatus() == 0 &&
184       mRenderStatus.NeedsUpdate() == false &&
185       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
186   {
187     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
188   }
189
190   unsigned int nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
191   float elapsedSeconds = intervalMilliseconds / 1e3f;
192
193   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false );
194
195   GetRenderController().Initialize();
196
197   mLastVSyncTime = nextVSyncTime;
198 }
199
200 bool TestApplication::Render( unsigned int intervalMilliseconds, const char* location )
201 {
202   DoUpdate( intervalMilliseconds, location );
203   mCore->Render( mRenderStatus, false );
204
205   mFrame++;
206
207   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
208 }
209
210 unsigned int TestApplication::GetUpdateStatus()
211 {
212   return mStatus.KeepUpdating();
213 }
214
215 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
216 {
217   DoUpdate( intervalMilliseconds );
218   return mStatus.KeepUpdating();
219 }
220
221 bool TestApplication::GetRenderNeedsUpdate()
222 {
223   return mRenderStatus.NeedsUpdate();
224 }
225
226 bool TestApplication::RenderOnly( )
227 {
228   // Update Time values
229   mCore->Render( mRenderStatus, false );
230
231   mFrame++;
232
233   return mRenderStatus.NeedsUpdate();
234 }
235
236 void TestApplication::ResetContext()
237 {
238   mCore->ContextDestroyed();
239   mGlAbstraction.Initialize();
240   mCore->ContextCreated();
241 }
242
243 unsigned int TestApplication::Wait( unsigned int durationToWait )
244 {
245   int time = 0;
246
247   for(unsigned int i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
248   {
249     SendNotification();
250     Render(RENDER_FRAME_INTERVAL);
251     time += RENDER_FRAME_INTERVAL;
252   }
253   return time;
254 }
255
256 } // Namespace dali