[dali_1.5.0] 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 : mRenderSurface( NULL ),
31   mCore( NULL ),
32   mSurfaceWidth( surfaceWidth ),
33   mSurfaceHeight( surfaceHeight ),
34   mFrame( 0u ),
35   mDpi{ horizontalDpi, verticalDpi },
36   mLastVSyncTime(0u)
37 {
38   if( initialize )
39   {
40     Initialize();
41   }
42 }
43
44 void TestApplication::Initialize()
45 {
46   CreateCore();
47   CreateScene();
48   InitializeCore();
49 }
50
51 void TestApplication::CreateCore()
52 {
53   // We always need the first update!
54   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
55
56   mCore = Dali::Integration::Core::New( mRenderController,
57                                         mPlatformAbstraction,
58                                         mGlAbstraction,
59                                         mGlSyncAbstraction,
60                                         mGlContextHelperAbstraction,
61                                         Integration::RenderToFrameBuffer::FALSE,
62                                         Integration::DepthBufferAvailable::TRUE,
63                                         Integration::StencilBufferAvailable::TRUE );
64
65   mCore->ContextCreated();
66
67   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
68   Dali::Integration::Log::InstallLogFunction(logFunction);
69
70   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
71   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
72
73   Dali::Integration::Trace::LogContext( true, "Test" );
74 }
75
76 void TestApplication::CreateScene()
77 {
78   mRenderSurface = new TestRenderSurface( Dali::PositionSize( 0, 0, mSurfaceWidth, mSurfaceHeight ) );
79   mScene = Dali::Integration::Scene::New( *mRenderSurface );
80   mScene.SetDpi( Vector2( static_cast<float>( mDpi.x ), static_cast<float>( mDpi.y ) ) );
81 }
82
83 void TestApplication::InitializeCore()
84 {
85   mCore->SceneCreated();
86   mCore->Initialize();
87 }
88
89 TestApplication::~TestApplication()
90 {
91   Dali::Integration::Log::UninstallLogFunction();
92   delete mRenderSurface;
93   delete mCore;
94 }
95
96 void TestApplication::LogContext( bool start, const char* tag )
97 {
98   if( start )
99   {
100     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
101   }
102   else
103   {
104     fprintf(stderr, "INFO: Trace End: %s\n", tag);
105   }
106 }
107
108 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
109 {
110   if( mLoggingEnabled )
111   {
112     switch(level)
113     {
114       case Dali::Integration::Log::DebugInfo:
115         fprintf(stderr, "INFO: %s", message.c_str());
116         break;
117       case Dali::Integration::Log::DebugWarning:
118         fprintf(stderr, "WARN: %s", message.c_str());
119         break;
120       case Dali::Integration::Log::DebugError:
121         fprintf(stderr, "ERROR: %s", message.c_str());
122         break;
123       default:
124         fprintf(stderr, "DEFAULT: %s", message.c_str());
125         break;
126     }
127   }
128 }
129
130 Dali::Integration::Core& TestApplication::GetCore()
131 {
132   return *mCore;
133 }
134
135 TestPlatformAbstraction& TestApplication::GetPlatform()
136 {
137   return mPlatformAbstraction;
138 }
139
140 TestRenderController& TestApplication::GetRenderController()
141 {
142   return mRenderController;
143 }
144
145 TestGlAbstraction& TestApplication::GetGlAbstraction()
146 {
147   return mGlAbstraction;
148 }
149
150 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
151 {
152   return mGlSyncAbstraction;
153 }
154
155 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
156 {
157   return mGlContextHelperAbstraction;
158 }
159
160 void TestApplication::ProcessEvent(const Integration::Event& event)
161 {
162   mCore->QueueEvent(event);
163   mCore->ProcessEvents();
164 }
165
166 void TestApplication::SendNotification()
167 {
168   mCore->ProcessEvents();
169 }
170
171 void TestApplication::DoUpdate( uint32_t intervalMilliseconds, const char* location )
172 {
173   if( GetUpdateStatus() == 0 &&
174       mRenderStatus.NeedsUpdate() == false &&
175       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
176   {
177     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
178   }
179
180   uint32_t nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
181   float elapsedSeconds = static_cast<float>( intervalMilliseconds ) * 0.001f;
182
183   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false );
184
185   GetRenderController().Initialize();
186
187   mLastVSyncTime = nextVSyncTime;
188 }
189
190 bool TestApplication::Render( uint32_t intervalMilliseconds, const char* location )
191 {
192   DoUpdate( intervalMilliseconds, location );
193   mCore->Render( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
194
195   mFrame++;
196
197   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
198 }
199
200 uint32_t TestApplication::GetUpdateStatus()
201 {
202   return mStatus.KeepUpdating();
203 }
204
205 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
206 {
207   DoUpdate( intervalMilliseconds );
208   return mStatus.KeepUpdating();
209 }
210
211 bool TestApplication::GetRenderNeedsUpdate()
212 {
213   return mRenderStatus.NeedsUpdate();
214 }
215
216 bool TestApplication::RenderOnly( )
217 {
218   // Update Time values
219   mCore->Render( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
220
221   mFrame++;
222
223   return mRenderStatus.NeedsUpdate();
224 }
225
226 void TestApplication::ResetContext()
227 {
228   mCore->ContextDestroyed();
229   mGlAbstraction.Initialize();
230   mCore->ContextCreated();
231 }
232
233 uint32_t TestApplication::Wait( uint32_t durationToWait )
234 {
235   int time = 0;
236
237   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
238   {
239     SendNotification();
240     Render(RENDER_FRAME_INTERVAL);
241     time += RENDER_FRAME_INTERVAL;
242   }
243   return time;
244 }
245
246 } // Namespace dali