[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2024 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   mScenes.push_back(mScene);
90 }
91
92 void TestApplication::InitializeCore()
93 {
94   mCore->SceneCreated();
95   mCore->Initialize();
96 }
97
98 TestApplication::~TestApplication()
99 {
100   Dali::Integration::Log::UninstallLogFunction();
101   delete mCore;
102 }
103
104 void TestApplication::LogContext(bool start, const char* tag, const char* message)
105 {
106   if(start)
107   {
108     fprintf(stderr, "INFO: Trace Start: %s %s\n", tag, message ? message : "");
109   }
110   else
111   {
112     fprintf(stderr, "INFO: Trace End: %s %s\n", tag, message ? message : "");
113   }
114 }
115
116 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
117 {
118   if(mLoggingEnabled)
119   {
120     switch(level)
121     {
122       case Dali::Integration::Log::DEBUG:
123         fprintf(stderr, "DEBUG: %s", message.c_str());
124         break;
125       case Dali::Integration::Log::INFO:
126         fprintf(stderr, "INFO: %s", message.c_str());
127         break;
128       case Dali::Integration::Log::WARNING:
129         fprintf(stderr, "WARN: %s", message.c_str());
130         break;
131       case Dali::Integration::Log::ERROR:
132         fprintf(stderr, "ERROR: %s", message.c_str());
133         break;
134       default:
135         fprintf(stderr, "DEFAULT: %s", message.c_str());
136         break;
137     }
138   }
139 }
140
141 Dali::Integration::Core& TestApplication::GetCore()
142 {
143   return *mCore;
144 }
145
146 TestPlatformAbstraction& TestApplication::GetPlatform()
147 {
148   return mPlatformAbstraction;
149 }
150
151 TestRenderController& TestApplication::GetRenderController()
152 {
153   return mRenderController;
154 }
155
156 TestGraphicsController& TestApplication::GetGraphicsController()
157 {
158   return mGraphicsController;
159 }
160
161 TestGlAbstraction& TestApplication::GetGlAbstraction()
162 {
163   return static_cast<TestGlAbstraction&>(mGraphicsController.GetGlAbstraction());
164 }
165
166 TestGraphicsSyncImplementation& TestApplication::GetGraphicsSyncImpl()
167 {
168   return static_cast<TestGraphicsSyncImplementation&>(mGraphicsController.GetGraphicsSyncImpl());
169 }
170
171 void TestApplication::ProcessEvent(const Integration::Event& event)
172 {
173   mCore->QueueEvent(event);
174   mCore->ProcessEvents();
175 }
176
177 void TestApplication::SendNotification()
178 {
179   mCore->ProcessEvents();
180 }
181
182 void TestApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location, bool uploadOnly)
183 {
184   if(GetUpdateStatus() == 0 &&
185      mRenderStatus.NeedsUpdate() == false &&
186      !GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc))
187   {
188     fprintf(stderr, "WARNING - Update not required :%s\n", location == NULL ? "NULL" : location);
189   }
190
191   uint32_t nextVSyncTime  = mLastVSyncTime + intervalMilliseconds;
192   float    elapsedSeconds = static_cast<float>(intervalMilliseconds) * 0.001f;
193
194   mCore->Update(elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false, uploadOnly);
195
196   GetRenderController().Initialize();
197
198   mLastVSyncTime = nextVSyncTime;
199 }
200
201 bool TestApplication::Render(uint32_t intervalMilliseconds, const char* location, bool uploadOnly)
202 {
203   DoUpdate(intervalMilliseconds, location, uploadOnly);
204
205   // Reset the status
206   mRenderStatus.SetNeedsUpdate(false);
207   mRenderStatus.SetNeedsPostRender(false);
208
209   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
210   if(!uploadOnly)
211   {
212     for(auto&& scene : mScenes)
213     {
214       mCore->RenderScene(mRenderStatus, scene, true /*render the off-screen buffers*/);
215       mCore->RenderScene(mRenderStatus, scene, false /*render the surface*/);
216     }
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 bool TestApplication::RenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location)
247 {
248   DoUpdate(intervalMilliseconds, location);
249
250   // Reset the status
251   mRenderStatus.SetNeedsUpdate(false);
252   mRenderStatus.SetNeedsPostRender(false);
253
254   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
255
256   for(auto&& scene : mScenes)
257   {
258     std::vector<Rect<int>> damagedRects;
259     Rect<int>              clippingRect{};
260
261     mCore->PreRender(scene, damagedRects);
262     mCore->RenderScene(mRenderStatus, scene, true /*render the off-screen buffers*/);
263     for(auto&& iter : damagedRects)
264     {
265       clippingRect.Merge(iter);
266     }
267     mCore->RenderScene(mRenderStatus, scene, false /*render the surface*/, clippingRect);
268   }
269   mCore->PostRender();
270
271   mFrame++;
272
273   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
274 }
275
276 uint32_t TestApplication::GetUpdateStatus()
277 {
278   return mStatus.KeepUpdating();
279 }
280
281 bool TestApplication::UpdateOnly(uint32_t intervalMilliseconds)
282 {
283   DoUpdate(intervalMilliseconds);
284   return mStatus.KeepUpdating();
285 }
286
287 bool TestApplication::GetRenderNeedsUpdate()
288 {
289   return mRenderStatus.NeedsUpdate();
290 }
291
292 bool TestApplication::GetRenderNeedsPostRender()
293 {
294   return mRenderStatus.NeedsPostRender();
295 }
296
297 bool TestApplication::RenderOnly()
298 {
299   // Update Time values
300   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
301   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
302   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
303   mCore->PostRender();
304
305   mFrame++;
306
307   return mRenderStatus.NeedsUpdate();
308 }
309
310 void TestApplication::ResetContext()
311 {
312   mCore->ContextDestroyed();
313   mGraphicsController.Initialize();
314   mCore->ContextCreated();
315 }
316
317 uint32_t TestApplication::Wait(uint32_t durationToWait)
318 {
319   int time = 0;
320
321   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
322   {
323     SendNotification();
324     Render(RENDER_FRAME_INTERVAL);
325     time += RENDER_FRAME_INTERVAL;
326   }
327   return time;
328 }
329
330 void TestApplication::AddScene(Integration::Scene scene)
331 {
332   mScenes.push_back(scene);
333 }
334
335 void TestApplication::RemoveScene(Integration::Scene scene)
336 {
337   mScenes.erase(std::remove(mScenes.begin(), mScenes.end(), scene), mScenes.end());
338 }
339
340 } // namespace Dali