Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / test-application.cpp
1 /*
2  * Copyright (c) 2022 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 const Rect<int> TestApplication::DEFAULT_SURFACE_RECT = Rect<int>(0, 0, TestApplication::DEFAULT_SURFACE_WIDTH, TestApplication::DEFAULT_SURFACE_HEIGHT);
23
24 bool TestApplication::mLoggingEnabled = true;
25
26 TestApplication::TestApplication(uint32_t surfaceWidth,
27                                  uint32_t surfaceHeight,
28                                  uint32_t horizontalDpi,
29                                  uint32_t verticalDpi,
30                                  bool     initialize,
31                                  bool     enablePartialUpdate)
32 : mCore(NULL),
33   mSurfaceWidth(surfaceWidth),
34   mSurfaceHeight(surfaceHeight),
35   mFrame(0u),
36   mDpi{horizontalDpi, verticalDpi},
37   mLastVSyncTime(0u),
38   mPartialUpdateEnabled(enablePartialUpdate)
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                                        mGraphicsController,
61                                        Integration::RenderToFrameBuffer::FALSE,
62                                        Integration::DepthBufferAvailable::TRUE,
63                                        Integration::StencilBufferAvailable::TRUE,
64                                        mPartialUpdateEnabled ? Integration::PartialUpdateAvailable::TRUE : 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   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(mSurfaceWidth), static_cast<float>(mSurfaceHeight)));
80   mScene.SetDpi(Vector2(static_cast<float>(mDpi.x), static_cast<float>(mDpi.y)));
81
82   // Create render target for the scene
83   Graphics::RenderTargetCreateInfo rtInfo{};
84   rtInfo.SetExtent({mSurfaceWidth, mSurfaceHeight});
85   rtInfo.SetSurface(&mSurfaceWidth); // Can point to anything, really.
86
87   mScene.SetSurfaceRenderTarget(rtInfo);
88 }
89
90 void TestApplication::InitializeCore()
91 {
92   mCore->SceneCreated();
93   mCore->Initialize();
94 }
95
96 TestApplication::~TestApplication()
97 {
98   Dali::Integration::Log::UninstallLogFunction();
99   delete mCore;
100 }
101
102 void TestApplication::LogContext(bool start, const char* tag)
103 {
104   if(start)
105   {
106     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
107   }
108   else
109   {
110     fprintf(stderr, "INFO: Trace End: %s\n", tag);
111   }
112 }
113
114 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
115 {
116   if(mLoggingEnabled)
117   {
118     switch(level)
119     {
120       case Dali::Integration::Log::DEBUG:
121         fprintf(stderr, "DEBUG: %s", message.c_str());
122         break;
123       case Dali::Integration::Log::INFO:
124         fprintf(stderr, "INFO: %s", message.c_str());
125         break;
126       case Dali::Integration::Log::WARNING:
127         fprintf(stderr, "WARN: %s", message.c_str());
128         break;
129       case Dali::Integration::Log::ERROR:
130         fprintf(stderr, "ERROR: %s", message.c_str());
131         break;
132       default:
133         fprintf(stderr, "DEFAULT: %s", message.c_str());
134         break;
135     }
136   }
137 }
138
139 Dali::Integration::Core& TestApplication::GetCore()
140 {
141   return *mCore;
142 }
143
144 TestPlatformAbstraction& TestApplication::GetPlatform()
145 {
146   return mPlatformAbstraction;
147 }
148
149 TestRenderController& TestApplication::GetRenderController()
150 {
151   return mRenderController;
152 }
153
154 TestGraphicsController& TestApplication::GetGraphicsController()
155 {
156   return mGraphicsController;
157 }
158
159 TestGlAbstraction& TestApplication::GetGlAbstraction()
160 {
161   return static_cast<TestGlAbstraction&>(mGraphicsController.GetGlAbstraction());
162 }
163
164 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
165 {
166   return static_cast<TestGlContextHelperAbstraction&>(mGraphicsController.GetGlContextHelperAbstraction());
167 }
168
169 TestGraphicsSyncImplementation& TestApplication::GetGraphicsSyncImpl()
170 {
171   return static_cast<TestGraphicsSyncImplementation&>(mGraphicsController.GetGraphicsSyncImpl());
172 }
173
174 void TestApplication::ProcessEvent(const Integration::Event& event)
175 {
176   mCore->QueueEvent(event);
177   mCore->ProcessEvents();
178 }
179
180 void TestApplication::SendNotification()
181 {
182   mCore->ProcessEvents();
183 }
184
185 void TestApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location, bool uploadOnly)
186 {
187   if(GetUpdateStatus() == 0 &&
188      mRenderStatus.NeedsUpdate() == false &&
189      !GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc))
190   {
191     fprintf(stderr, "WARNING - Update not required :%s\n", location == NULL ? "NULL" : location);
192   }
193
194   uint32_t nextVSyncTime  = mLastVSyncTime + intervalMilliseconds;
195   float    elapsedSeconds = static_cast<float>(intervalMilliseconds) * 0.001f;
196
197   mCore->Update(elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false, uploadOnly);
198
199   GetRenderController().Initialize();
200
201   mLastVSyncTime = nextVSyncTime;
202 }
203
204 bool TestApplication::Render(uint32_t intervalMilliseconds, const char* location, bool uploadOnly)
205 {
206   DoUpdate(intervalMilliseconds, location, uploadOnly);
207
208   // Reset the status
209   mRenderStatus.SetNeedsUpdate(false);
210   mRenderStatus.SetNeedsPostRender(false);
211
212   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
213   if(!uploadOnly)
214   {
215     mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
216     mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
217   }
218   mCore->PostRender();
219
220   mFrame++;
221
222   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
223 }
224
225 bool TestApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
226 {
227   DoUpdate(intervalMilliseconds, location);
228
229   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
230   mCore->PreRender(mScene, damagedRects);
231
232   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
233 }
234
235 bool TestApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
236 {
237   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
238   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
239   mCore->PostRender();
240
241   mFrame++;
242
243   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
244 }
245
246 uint32_t TestApplication::GetUpdateStatus()
247 {
248   return mStatus.KeepUpdating();
249 }
250
251 bool TestApplication::UpdateOnly(uint32_t intervalMilliseconds)
252 {
253   DoUpdate(intervalMilliseconds);
254   return mStatus.KeepUpdating();
255 }
256
257 bool TestApplication::GetRenderNeedsUpdate()
258 {
259   return mRenderStatus.NeedsUpdate();
260 }
261
262 bool TestApplication::GetRenderNeedsPostRender()
263 {
264   return mRenderStatus.NeedsPostRender();
265 }
266
267 bool TestApplication::RenderOnly()
268 {
269   // Update Time values
270   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
271   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
272   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
273   mCore->PostRender();
274
275   mFrame++;
276
277   return mRenderStatus.NeedsUpdate();
278 }
279
280 void TestApplication::ResetContext()
281 {
282   mCore->ContextDestroyed();
283   mGraphicsController.Initialize();
284   mCore->ContextCreated();
285 }
286
287 uint32_t TestApplication::Wait(uint32_t durationToWait)
288 {
289   int time = 0;
290
291   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
292   {
293     SendNotification();
294     Render(RENDER_FRAME_INTERVAL);
295     time += RENDER_FRAME_INTERVAL;
296   }
297   return time;
298 }
299
300 } // namespace Dali