f3029fa2d83700a36bbed7ebaa000d51ee8968cd
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2019 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 bool TestApplication::mLoggingEnabled = true;
24
25 TestApplication::TestApplication( uint32_t surfaceWidth,
26                                   uint32_t surfaceHeight,
27                                   uint32_t  horizontalDpi,
28                                   uint32_t  verticalDpi,
29                                   ResourcePolicy::DataRetention policy,
30                                   bool initialize )
31 : mCore( NULL ),
32   mSurfaceWidth( surfaceWidth ),
33   mSurfaceHeight( surfaceHeight ),
34   mFrame( 0u ),
35   mDpi{ horizontalDpi, verticalDpi },
36   mLastVSyncTime(0u),
37   mDataRetentionPolicy( policy )
38 {
39   if( initialize )
40   {
41     Initialize();
42   }
43 }
44
45 void TestApplication::Initialize()
46 {
47   CreateCore();
48   CreateScene();
49   InitializeCore();
50 }
51
52 void TestApplication::CreateCore()
53 {
54   // We always need the first update!
55   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
56
57   mCore = Dali::Integration::Core::New( mRenderController,
58                                         mPlatformAbstraction,
59                                         mGlAbstraction,
60                                         mGlSyncAbstraction,
61                                         mGlContextHelperAbstraction,
62                                         mDataRetentionPolicy,
63                                         Integration::RenderToFrameBuffer::FALSE,
64                                         Integration::DepthBufferAvailable::TRUE,
65                                         Integration::StencilBufferAvailable::TRUE );
66
67   mCore->ContextCreated();
68
69   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
70   Dali::Integration::Log::InstallLogFunction(logFunction);
71
72   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
73   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
74
75   Dali::Integration::Trace::LogContext( true, "Test" );
76 }
77
78 void TestApplication::CreateScene()
79 {
80   mRenderSurface = new TestRenderSurface( Dali::PositionSize( 0, 0, mSurfaceWidth, mSurfaceHeight ) );
81   mScene = Dali::Integration::Scene::New( *mRenderSurface );
82   mScene.SetDpi( Vector2( static_cast<float>( mDpi.x ), static_cast<float>( mDpi.y ) ) );
83 }
84
85 void TestApplication::InitializeCore()
86 {
87   mCore->SceneCreated();
88   mCore->Initialize();
89 }
90
91 TestApplication::~TestApplication()
92 {
93   Dali::Integration::Log::UninstallLogFunction();
94   delete mRenderSurface;
95   delete mCore;
96 }
97
98 void TestApplication::LogContext( bool start, const char* tag )
99 {
100   if( start )
101   {
102     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
103   }
104   else
105   {
106     fprintf(stderr, "INFO: Trace End: %s\n", tag);
107   }
108 }
109
110 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
111 {
112   if( mLoggingEnabled )
113   {
114     switch(level)
115     {
116       case Dali::Integration::Log::DebugInfo:
117         fprintf(stderr, "INFO: %s", message.c_str());
118         break;
119       case Dali::Integration::Log::DebugWarning:
120         fprintf(stderr, "WARN: %s", message.c_str());
121         break;
122       case Dali::Integration::Log::DebugError:
123         fprintf(stderr, "ERROR: %s", message.c_str());
124         break;
125       default:
126         fprintf(stderr, "DEFAULT: %s", message.c_str());
127         break;
128     }
129   }
130 }
131
132 Dali::Integration::Core& TestApplication::GetCore()
133 {
134   return *mCore;
135 }
136
137 TestPlatformAbstraction& TestApplication::GetPlatform()
138 {
139   return mPlatformAbstraction;
140 }
141
142 TestRenderController& TestApplication::GetRenderController()
143 {
144   return mRenderController;
145 }
146
147 TestGlAbstraction& TestApplication::GetGlAbstraction()
148 {
149   return mGlAbstraction;
150 }
151
152 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
153 {
154   return mGlSyncAbstraction;
155 }
156
157 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
158 {
159   return mGlContextHelperAbstraction;
160 }
161
162 void TestApplication::ProcessEvent(const Integration::Event& event)
163 {
164   mCore->QueueEvent(event);
165   mCore->ProcessEvents();
166 }
167
168 void TestApplication::SendNotification()
169 {
170   mCore->ProcessEvents();
171 }
172
173 void TestApplication::DoUpdate( uint32_t intervalMilliseconds, const char* location )
174 {
175   if( GetUpdateStatus() == 0 &&
176       mRenderStatus.NeedsUpdate() == false &&
177       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
178   {
179     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
180   }
181
182   uint32_t nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
183   float elapsedSeconds = static_cast<float>( intervalMilliseconds ) * 0.001f;
184
185   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false );
186
187   GetRenderController().Initialize();
188
189   mLastVSyncTime = nextVSyncTime;
190 }
191
192 bool TestApplication::Render( uint32_t intervalMilliseconds, const char* location )
193 {
194   DoUpdate( intervalMilliseconds, location );
195   mCore->Render( mRenderStatus, false );
196
197   mFrame++;
198
199   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
200 }
201
202 uint32_t TestApplication::GetUpdateStatus()
203 {
204   return mStatus.KeepUpdating();
205 }
206
207 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
208 {
209   DoUpdate( intervalMilliseconds );
210   return mStatus.KeepUpdating();
211 }
212
213 bool TestApplication::GetRenderNeedsUpdate()
214 {
215   return mRenderStatus.NeedsUpdate();
216 }
217
218 bool TestApplication::RenderOnly( )
219 {
220   // Update Time values
221   mCore->Render( mRenderStatus, false );
222
223   mFrame++;
224
225   return mRenderStatus.NeedsUpdate();
226 }
227
228 void TestApplication::ResetContext()
229 {
230   mCore->ContextDestroyed();
231   mGlAbstraction.Initialize();
232   mCore->ContextCreated();
233 }
234
235 uint32_t TestApplication::Wait( uint32_t durationToWait )
236 {
237   int time = 0;
238
239   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
240   {
241     SendNotification();
242     Render(RENDER_FRAME_INTERVAL);
243     time += RENDER_FRAME_INTERVAL;
244   }
245   return time;
246 }
247
248 } // Namespace dali