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