[dali_1.4.38] 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 : mRenderSurface( NULL ),
32   mCore( NULL ),
33   mSurfaceWidth( surfaceWidth ),
34   mSurfaceHeight( surfaceHeight ),
35   mFrame( 0u ),
36   mDpi{ horizontalDpi, verticalDpi },
37   mLastVSyncTime(0u),
38   mDataRetentionPolicy( policy )
39 {
40   if( initialize )
41   {
42     Initialize();
43   }
44 }
45
46 void TestApplication::Initialize()
47 {
48   CreateCore();
49   CreateScene();
50   InitializeCore();
51 }
52
53 void TestApplication::CreateCore()
54 {
55   // We always need the first update!
56   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
57
58   mCore = Dali::Integration::Core::New( mRenderController,
59                                         mPlatformAbstraction,
60                                         mGlAbstraction,
61                                         mGlSyncAbstraction,
62                                         mGlContextHelperAbstraction,
63                                         mDataRetentionPolicy,
64                                         Integration::RenderToFrameBuffer::FALSE,
65                                         Integration::DepthBufferAvailable::TRUE,
66                                         Integration::StencilBufferAvailable::TRUE );
67
68   mCore->ContextCreated();
69
70   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
71   Dali::Integration::Log::InstallLogFunction(logFunction);
72
73   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
74   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
75
76   Dali::Integration::Trace::LogContext( true, "Test" );
77 }
78
79 void TestApplication::CreateScene()
80 {
81   mRenderSurface = new TestRenderSurface( Dali::PositionSize( 0, 0, mSurfaceWidth, mSurfaceHeight ) );
82   mScene = Dali::Integration::Scene::New( *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 /*do not force clear*/, false /*do not skip rendering*/ );
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 /*do not force clear*/, false /*do not skip rendering*/ );
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