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