[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-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                                         Integration::PartialUpdateAvailable::FALSE );
65
66   mCore->ContextCreated();
67
68   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
69   Dali::Integration::Log::InstallLogFunction(logFunction);
70
71   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
72   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
73
74   Dali::Integration::Trace::LogContext( true, "Test" );
75 }
76
77 void TestApplication::CreateScene()
78 {
79   mRenderSurface = new TestRenderSurface( Dali::PositionSize( 0, 0, mSurfaceWidth, mSurfaceHeight ) );
80   mScene = Dali::Integration::Scene::New( *mRenderSurface );
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 mRenderSurface;
94   delete mCore;
95 }
96
97 void TestApplication::LogContext( bool start, const char* tag )
98 {
99   if( start )
100   {
101     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
102   }
103   else
104   {
105     fprintf(stderr, "INFO: Trace End: %s\n", tag);
106   }
107 }
108
109 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
110 {
111   if( mLoggingEnabled )
112   {
113     switch(level)
114     {
115       case Dali::Integration::Log::DebugInfo:
116         fprintf(stderr, "INFO: %s", message.c_str());
117         break;
118       case Dali::Integration::Log::DebugWarning:
119         fprintf(stderr, "WARN: %s", message.c_str());
120         break;
121       case Dali::Integration::Log::DebugError:
122         fprintf(stderr, "ERROR: %s", message.c_str());
123         break;
124       default:
125         fprintf(stderr, "DEFAULT: %s", message.c_str());
126         break;
127     }
128   }
129 }
130
131 Dali::Integration::Core& TestApplication::GetCore()
132 {
133   return *mCore;
134 }
135
136 TestPlatformAbstraction& TestApplication::GetPlatform()
137 {
138   return mPlatformAbstraction;
139 }
140
141 TestRenderController& TestApplication::GetRenderController()
142 {
143   return mRenderController;
144 }
145
146 TestGlAbstraction& TestApplication::GetGlAbstraction()
147 {
148   return mGlAbstraction;
149 }
150
151 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
152 {
153   return mGlSyncAbstraction;
154 }
155
156 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
157 {
158   return mGlContextHelperAbstraction;
159 }
160
161 void TestApplication::ProcessEvent(const Integration::Event& event)
162 {
163   mCore->QueueEvent(event);
164   mCore->ProcessEvents();
165 }
166
167 void TestApplication::SendNotification()
168 {
169   mCore->ProcessEvents();
170 }
171
172 void TestApplication::DoUpdate( uint32_t intervalMilliseconds, const char* location )
173 {
174   if( GetUpdateStatus() == 0 &&
175       mRenderStatus.NeedsUpdate() == false &&
176       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
177   {
178     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
179   }
180
181   uint32_t nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
182   float elapsedSeconds = static_cast<float>( intervalMilliseconds ) * 0.001f;
183
184   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false );
185
186   GetRenderController().Initialize();
187
188   mLastVSyncTime = nextVSyncTime;
189 }
190
191 bool TestApplication::Render( uint32_t intervalMilliseconds, const char* location )
192 {
193   DoUpdate( intervalMilliseconds, location );
194   mCore->Render( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
195
196   mFrame++;
197
198   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
199 }
200
201 uint32_t TestApplication::GetUpdateStatus()
202 {
203   return mStatus.KeepUpdating();
204 }
205
206 bool TestApplication::UpdateOnly( uint32_t intervalMilliseconds  )
207 {
208   DoUpdate( intervalMilliseconds );
209   return mStatus.KeepUpdating();
210 }
211
212 bool TestApplication::GetRenderNeedsUpdate()
213 {
214   return mRenderStatus.NeedsUpdate();
215 }
216
217 bool TestApplication::RenderOnly( )
218 {
219   // Update Time values
220   mCore->Render( mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/ );
221
222   mFrame++;
223
224   return mRenderStatus.NeedsUpdate();
225 }
226
227 void TestApplication::ResetContext()
228 {
229   mCore->ContextDestroyed();
230   mGlAbstraction.Initialize();
231   mCore->ContextCreated();
232 }
233
234 uint32_t TestApplication::Wait( uint32_t durationToWait )
235 {
236   int time = 0;
237
238   for(uint32_t i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
239   {
240     SendNotification();
241     Render(RENDER_FRAME_INTERVAL);
242     time += RENDER_FRAME_INTERVAL;
243   }
244   return time;
245 }
246
247 } // Namespace dali