7ffb5e5531f274773370a21db0f4efa7aee7e7b1
[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( Vector2( static_cast<float>( mSurfaceWidth ), static_cast<float>( mSurfaceHeight ) ) );
82   mScene.SetSurface( *mRenderSurface );
83   mScene.SetDpi( Vector2( static_cast<float>( mDpi.x ), static_cast<float>( mDpi.y ) ) );
84 }
85
86 void TestApplication::InitializeCore()
87 {
88   mCore->SceneCreated();
89   mCore->Initialize();
90 }
91
92 TestApplication::~TestApplication()
93 {
94   Dali::Integration::Log::UninstallLogFunction();
95   delete mRenderSurface;
96   delete mCore;
97 }
98
99 void TestApplication::LogContext( bool start, const char* tag )
100 {
101   if( start )
102   {
103     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
104   }
105   else
106   {
107     fprintf(stderr, "INFO: Trace End: %s\n", tag);
108   }
109 }
110
111 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
112 {
113   if( mLoggingEnabled )
114   {
115     switch(level)
116     {
117       case Dali::Integration::Log::DebugInfo:
118         fprintf(stderr, "INFO: %s", message.c_str());
119         break;
120       case Dali::Integration::Log::DebugWarning:
121         fprintf(stderr, "WARN: %s", message.c_str());
122         break;
123       case Dali::Integration::Log::DebugError:
124         fprintf(stderr, "ERROR: %s", message.c_str());
125         break;
126       default:
127         fprintf(stderr, "DEFAULT: %s", message.c_str());
128         break;
129     }
130   }
131 }
132
133 Dali::Integration::Core& TestApplication::GetCore()
134 {
135   return *mCore;
136 }
137
138 TestPlatformAbstraction& TestApplication::GetPlatform()
139 {
140   return mPlatformAbstraction;
141 }
142
143 TestRenderController& TestApplication::GetRenderController()
144 {
145   return mRenderController;
146 }
147
148 TestGlAbstraction& TestApplication::GetGlAbstraction()
149 {
150   return mGlAbstraction;
151 }
152
153 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
154 {
155   return mGlSyncAbstraction;
156 }
157
158 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
159 {
160   return mGlContextHelperAbstraction;
161 }
162
163 void TestApplication::ProcessEvent(const Integration::Event& event)
164 {
165   mCore->QueueEvent(event);
166   mCore->ProcessEvents();
167 }
168
169 void TestApplication::SendNotification()
170 {
171   mCore->ProcessEvents();
172 }
173
174 void TestApplication::DoUpdate( uint32_t intervalMilliseconds, const char* location )
175 {
176   if( GetUpdateStatus() == 0 &&
177       mRenderStatus.NeedsUpdate() == false &&
178       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
179   {
180     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
181   }
182
183   uint32_t nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
184   float elapsedSeconds = static_cast<float>( intervalMilliseconds ) * 0.001f;
185
186   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false );
187
188   GetRenderController().Initialize();
189
190   mLastVSyncTime = nextVSyncTime;
191 }
192
193 bool TestApplication::Render( uint32_t intervalMilliseconds, const char* location )
194 {
195   DoUpdate( intervalMilliseconds, location );
196   mCore->Render( mRenderStatus, false );
197
198   mFrame++;
199
200   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
201 }
202
203 uint32_t TestApplication::GetUpdateStatus()
204 {
205   return mStatus.KeepUpdating();
206 }
207
208 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
209 {
210   DoUpdate( intervalMilliseconds );
211   return mStatus.KeepUpdating();
212 }
213
214 bool TestApplication::GetRenderNeedsUpdate()
215 {
216   return mRenderStatus.NeedsUpdate();
217 }
218
219 bool TestApplication::RenderOnly( )
220 {
221   // Update Time values
222   mCore->Render( mRenderStatus, false );
223
224   mFrame++;
225
226   return mRenderStatus.NeedsUpdate();
227 }
228
229 void TestApplication::ResetContext()
230 {
231   mCore->ContextDestroyed();
232   mGlAbstraction.Initialize();
233   mCore->ContextCreated();
234 }
235
236 uint32_t TestApplication::Wait( uint32_t durationToWait )
237 {
238   int time = 0;
239
240   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
241   {
242     SendNotification();
243     Render(RENDER_FRAME_INTERVAL);
244     time += RENDER_FRAME_INTERVAL;
245   }
246   return time;
247 }
248
249 } // Namespace dali