[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) 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 : mRenderSurface( NULL ),
32   mCore( NULL ),
33   mSurfaceWidth( surfaceWidth ),
34   mSurfaceHeight( surfaceHeight ),
35   mFrame( 0u ),
36   mDpi{ horizontalDpi, verticalDpi },
37   mLastVSyncTime(0u),
38   mDataRetentionPolicy( policy )
39 {
40   if( initialize )
41   {
42     Initialize();
43   }
44 }
45
46 void TestApplication::Initialize()
47 {
48   CreateCore();
49   CreateScene();
50   InitializeCore();
51 }
52
53 void TestApplication::CreateCore()
54 {
55   // We always need the first update!
56   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
57
58   mCore = Dali::Integration::Core::New( mRenderController,
59                                         mPlatformAbstraction,
60                                         mGlAbstraction,
61                                         mGlSyncAbstraction,
62                                         mGlContextHelperAbstraction,
63                                         mDataRetentionPolicy,
64                                         Integration::RenderToFrameBuffer::FALSE,
65                                         Integration::DepthBufferAvailable::TRUE,
66                                         Integration::StencilBufferAvailable::TRUE,
67                                         Integration::PartialUpdateAvailable::FALSE );
68
69   mCore->ContextCreated();
70
71   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
72   Dali::Integration::Log::InstallLogFunction(logFunction);
73
74   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
75   Dali::Integration::Trace::InstallLogContextFunction( logContextFunction );
76
77   Dali::Integration::Trace::LogContext( true, "Test" );
78 }
79
80 void TestApplication::CreateScene()
81 {
82   mRenderSurface = new TestRenderSurface( Dali::PositionSize( 0, 0, mSurfaceWidth, mSurfaceHeight ) );
83   mScene = Dali::Integration::Scene::New( *mRenderSurface );
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