Sync common test files
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2020 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                                   bool initialize )
30 : mCore( NULL ),
31   mSurfaceWidth( surfaceWidth ),
32   mSurfaceHeight( surfaceHeight ),
33   mFrame( 0u ),
34   mDpi{ horizontalDpi, verticalDpi },
35   mLastVSyncTime(0u)
36 {
37   if( initialize )
38   {
39     Initialize();
40   }
41 }
42
43 void TestApplication::Initialize()
44 {
45   CreateCore();
46   CreateScene();
47   InitializeCore();
48 }
49
50 void TestApplication::CreateCore()
51 {
52   // We always need the first update!
53   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
54
55   mCore = Dali::Integration::Core::New( mRenderController,
56                                         mPlatformAbstraction,
57                                         mGlAbstraction,
58                                         mGlSyncAbstraction,
59                                         mGlContextHelperAbstraction,
60                                         Integration::RenderToFrameBuffer::FALSE,
61                                         Integration::DepthBufferAvailable::TRUE,
62                                         Integration::StencilBufferAvailable::TRUE );
63
64   mCore->ContextCreated();
65
66   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
67   Dali::Integration::Log::InstallLogFunction(logFunction);
68
69   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
70   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
71
72   Dali::Integration::Trace::LogContext( true, "Test" );
73 }
74
75 void TestApplication::CreateScene()
76 {
77   mScene = Dali::Integration::Scene::New( Size( static_cast<float>( mSurfaceWidth ), static_cast<float>( mSurfaceHeight ) ) );
78   mScene.SetDpi( Vector2( static_cast<float>( mDpi.x ), static_cast<float>( mDpi.y ) ) );
79 }
80
81 void TestApplication::InitializeCore()
82 {
83   mCore->SceneCreated();
84   mCore->Initialize();
85 }
86
87 TestApplication::~TestApplication()
88 {
89   Dali::Integration::Log::UninstallLogFunction();
90   delete mCore;
91 }
92
93 void TestApplication::LogContext( bool start, const char* tag )
94 {
95   if( start )
96   {
97     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
98   }
99   else
100   {
101     fprintf(stderr, "INFO: Trace End: %s\n", tag);
102   }
103 }
104
105 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
106 {
107   if( mLoggingEnabled )
108   {
109     switch(level)
110     {
111       case Dali::Integration::Log::DebugInfo:
112         fprintf(stderr, "INFO: %s", message.c_str());
113         break;
114       case Dali::Integration::Log::DebugWarning:
115         fprintf(stderr, "WARN: %s", message.c_str());
116         break;
117       case Dali::Integration::Log::DebugError:
118         fprintf(stderr, "ERROR: %s", message.c_str());
119         break;
120       default:
121         fprintf(stderr, "DEFAULT: %s", message.c_str());
122         break;
123     }
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 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
153 {
154   return mGlContextHelperAbstraction;
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::DoUpdate( uint32_t intervalMilliseconds, const char* location )
169 {
170   if( GetUpdateStatus() == 0 &&
171       mRenderStatus.NeedsUpdate() == false &&
172       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
173   {
174     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
175   }
176
177   uint32_t nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
178   float elapsedSeconds = static_cast<float>( intervalMilliseconds ) * 0.001f;
179
180   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false );
181
182   GetRenderController().Initialize();
183
184   mLastVSyncTime = nextVSyncTime;
185 }
186
187 bool TestApplication::Render( uint32_t intervalMilliseconds, const char* location )
188 {
189   DoUpdate( intervalMilliseconds, location );
190
191   // Reset the status
192   mRenderStatus.SetNeedsUpdate( false );
193   mRenderStatus.SetNeedsPostRender( false );
194
195   mCore->PreRender( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
196   mCore->RenderScene( mRenderStatus, mScene, true /*render the off-screen buffers*/);
197   mCore->RenderScene( mRenderStatus, mScene, false /*render the surface*/);
198   mCore->PostRender( false /*do not skip rendering*/ );
199
200   mFrame++;
201
202   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
203 }
204
205 uint32_t TestApplication::GetUpdateStatus()
206 {
207   return mStatus.KeepUpdating();
208 }
209
210 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
211 {
212   DoUpdate( intervalMilliseconds );
213   return mStatus.KeepUpdating();
214 }
215
216 bool TestApplication::GetRenderNeedsUpdate()
217 {
218   return mRenderStatus.NeedsUpdate();
219 }
220
221 bool TestApplication::GetRenderNeedsPostRender()
222 {
223   return mRenderStatus.NeedsPostRender();
224 }
225
226 bool TestApplication::RenderOnly( )
227 {
228   // Update Time values
229   mCore->PreRender( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
230   mCore->RenderScene( mRenderStatus, mScene, true /*render the off-screen buffers*/);
231   mCore->RenderScene( mRenderStatus, mScene, false /*render the surface*/);
232   mCore->PostRender( false /*do not skip rendering*/ );
233
234   mFrame++;
235
236   return mRenderStatus.NeedsUpdate();
237 }
238
239 void TestApplication::ResetContext()
240 {
241   mCore->ContextDestroyed();
242   mGlAbstraction.Initialize();
243   mCore->ContextCreated();
244 }
245
246 uint32_t TestApplication::Wait( uint32_t durationToWait )
247 {
248   int time = 0;
249
250   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
251   {
252     SendNotification();
253     Render(RENDER_FRAME_INTERVAL);
254     time += RENDER_FRAME_INTERVAL;
255   }
256   return time;
257 }
258
259 } // Namespace dali