[dali_1.9.6] Merge branch 'devel/master'
[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   mCore->PreRender( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
192   mCore->RenderScene( mScene, true /*render the off-screen buffers*/);
193   mCore->RenderScene( mScene, false /*render the surface*/);
194   mCore->PostRender( false /*do not skip rendering*/ );
195
196   mFrame++;
197
198   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
199 }
200
201 uint32_t TestApplication::GetUpdateStatus()
202 {
203   return mStatus.KeepUpdating();
204 }
205
206 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
207 {
208   DoUpdate( intervalMilliseconds );
209   return mStatus.KeepUpdating();
210 }
211
212 bool TestApplication::GetRenderNeedsUpdate()
213 {
214   return mRenderStatus.NeedsUpdate();
215 }
216
217 bool TestApplication::RenderOnly( )
218 {
219   // Update Time values
220   mCore->PreRender( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
221   mCore->RenderScene( mScene, true /*render the off-screen buffers*/);
222   mCore->RenderScene( mScene, false /*render the surface*/);
223   mCore->PostRender( false /*do not skip rendering*/ );
224
225   mFrame++;
226
227   return mRenderStatus.NeedsUpdate();
228 }
229
230 void TestApplication::ResetContext()
231 {
232   mCore->ContextDestroyed();
233   mGlAbstraction.Initialize();
234   mCore->ContextCreated();
235 }
236
237 uint32_t TestApplication::Wait( uint32_t durationToWait )
238 {
239   int time = 0;
240
241   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
242   {
243     SendNotification();
244     Render(RENDER_FRAME_INTERVAL);
245     time += RENDER_FRAME_INTERVAL;
246   }
247   return time;
248 }
249
250 } // Namespace dali