Add partial update support.
[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                                   bool enablePartialUpdate )
31 : mCore( NULL ),
32   mSurfaceWidth( surfaceWidth ),
33   mSurfaceHeight( surfaceHeight ),
34   mFrame( 0u ),
35   mDpi{ horizontalDpi, verticalDpi },
36   mLastVSyncTime(0u),
37   mPartialUpdateEnabled(enablePartialUpdate)
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                                         Integration::RenderToFrameBuffer::FALSE,
63                                         Integration::DepthBufferAvailable::TRUE,
64                                         Integration::StencilBufferAvailable::TRUE,
65                                         mPartialUpdateEnabled ? Integration::PartialUpdateAvailable::TRUE : Integration::PartialUpdateAvailable::FALSE );
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   mScene = Dali::Integration::Scene::New( Size( static_cast<float>( mSurfaceWidth ), static_cast<float>( mSurfaceHeight ) ) );
81   mScene.SetDpi( Vector2( static_cast<float>( mDpi.x ), static_cast<float>( mDpi.y ) ) );
82 }
83
84 void TestApplication::InitializeCore()
85 {
86   mCore->SceneCreated();
87   mCore->Initialize();
88 }
89
90 TestApplication::~TestApplication()
91 {
92   Dali::Integration::Log::UninstallLogFunction();
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
194   // Reset the status
195   mRenderStatus.SetNeedsUpdate( false );
196   mRenderStatus.SetNeedsPostRender( false );
197
198   mCore->PreRender( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
199   mCore->RenderScene( mRenderStatus, mScene, true /*render the off-screen buffers*/ );
200   mCore->RenderScene( mRenderStatus, mScene, false /*render the surface*/ );
201   mCore->PostRender( false /*do not skip rendering*/ );
202
203   mFrame++;
204
205   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
206 }
207
208 bool TestApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
209 {
210   DoUpdate(intervalMilliseconds, location);
211
212   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
213   mCore->PreRender(mScene, damagedRects);
214
215   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
216 }
217
218 bool TestApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
219 {
220   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/, clippingRect);
221   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
222   mCore->PostRender(false /*do not skip rendering*/);
223
224   mFrame++;
225
226   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
227 }
228
229 uint32_t TestApplication::GetUpdateStatus()
230 {
231   return mStatus.KeepUpdating();
232 }
233
234 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
235 {
236   DoUpdate( intervalMilliseconds );
237   return mStatus.KeepUpdating();
238 }
239
240 bool TestApplication::GetRenderNeedsUpdate()
241 {
242   return mRenderStatus.NeedsUpdate();
243 }
244
245 bool TestApplication::GetRenderNeedsPostRender()
246 {
247   return mRenderStatus.NeedsPostRender();
248 }
249
250 bool TestApplication::RenderOnly( )
251 {
252   // Update Time values
253   mCore->PreRender( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
254   mCore->RenderScene( mRenderStatus, mScene, true /*render the off-screen buffers*/ );
255   mCore->RenderScene( mRenderStatus, mScene, false /*render the surface*/ );
256   mCore->PostRender( false /*do not skip rendering*/ );
257
258   mFrame++;
259
260   return mRenderStatus.NeedsUpdate();
261 }
262
263 void TestApplication::ResetContext()
264 {
265   mCore->ContextDestroyed();
266   mGlAbstraction.Initialize();
267   mCore->ContextCreated();
268 }
269
270 uint32_t TestApplication::Wait( uint32_t durationToWait )
271 {
272   int time = 0;
273
274   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
275   {
276     SendNotification();
277     Render(RENDER_FRAME_INTERVAL);
278     time += RENDER_FRAME_INTERVAL;
279   }
280   return time;
281 }
282
283 } // Namespace dali