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