Merge "Detach skybox on setting an empty url" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2023 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 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
167 {
168   return static_cast<TestGlContextHelperAbstraction&>(mGraphicsController.GetGlContextHelperAbstraction());
169 }
170
171 TestGraphicsSyncImplementation& TestApplication::GetGraphicsSyncImpl()
172 {
173   return static_cast<TestGraphicsSyncImplementation&>(mGraphicsController.GetGraphicsSyncImpl());
174 }
175
176 void TestApplication::ProcessEvent(const Integration::Event& event)
177 {
178   mCore->QueueEvent(event);
179   mCore->ProcessEvents();
180 }
181
182 void TestApplication::SendNotification()
183 {
184   mCore->ProcessEvents();
185 }
186
187 void TestApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location, bool uploadOnly)
188 {
189   if(GetUpdateStatus() == 0 &&
190      mRenderStatus.NeedsUpdate() == false &&
191      !GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc))
192   {
193     fprintf(stderr, "WARNING - Update not required :%s\n", location == NULL ? "NULL" : location);
194   }
195
196   uint32_t nextVSyncTime  = mLastVSyncTime + intervalMilliseconds;
197   float    elapsedSeconds = static_cast<float>(intervalMilliseconds) * 0.001f;
198
199   mCore->Update(elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false, uploadOnly);
200
201   GetRenderController().Initialize();
202
203   mLastVSyncTime = nextVSyncTime;
204 }
205
206 bool TestApplication::Render(uint32_t intervalMilliseconds, const char* location, bool uploadOnly)
207 {
208   DoUpdate(intervalMilliseconds, location, uploadOnly);
209
210   // Reset the status
211   mRenderStatus.SetNeedsUpdate(false);
212   mRenderStatus.SetNeedsPostRender(false);
213
214   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
215   if(!uploadOnly)
216   {
217     for(auto&& scene : mScenes)
218     {
219       mCore->RenderScene(mRenderStatus, scene, true /*render the off-screen buffers*/);
220       mCore->RenderScene(mRenderStatus, scene, false /*render the surface*/);
221     }
222   }
223   mCore->PostRender();
224
225   mFrame++;
226
227   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
228 }
229
230 bool TestApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
231 {
232   DoUpdate(intervalMilliseconds, location);
233
234   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
235   mCore->PreRender(mScene, damagedRects);
236
237   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
238 }
239
240 bool TestApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
241 {
242   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
243   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
244   mCore->PostRender();
245
246   mFrame++;
247
248   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
249 }
250
251 bool TestApplication::RenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location)
252 {
253   DoUpdate(intervalMilliseconds, location);
254
255   // Reset the status
256   mRenderStatus.SetNeedsUpdate(false);
257   mRenderStatus.SetNeedsPostRender(false);
258
259   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
260
261   for(auto&& scene : mScenes)
262   {
263     std::vector<Rect<int>> damagedRects;
264     Rect<int>              clippingRect{};
265
266     mCore->PreRender(scene, damagedRects);
267     mCore->RenderScene(mRenderStatus, scene, true /*render the off-screen buffers*/);
268     for(auto&& iter : damagedRects)
269     {
270       clippingRect.Merge(iter);
271     }
272     mCore->RenderScene(mRenderStatus, scene, false /*render the surface*/, clippingRect);
273   }
274   mCore->PostRender();
275
276   mFrame++;
277
278   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
279 }
280
281 uint32_t TestApplication::GetUpdateStatus()
282 {
283   return mStatus.KeepUpdating();
284 }
285
286 bool TestApplication::UpdateOnly(uint32_t intervalMilliseconds)
287 {
288   DoUpdate(intervalMilliseconds);
289   return mStatus.KeepUpdating();
290 }
291
292 bool TestApplication::GetRenderNeedsUpdate()
293 {
294   return mRenderStatus.NeedsUpdate();
295 }
296
297 bool TestApplication::GetRenderNeedsPostRender()
298 {
299   return mRenderStatus.NeedsPostRender();
300 }
301
302 bool TestApplication::RenderOnly()
303 {
304   // Update Time values
305   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
306   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
307   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
308   mCore->PostRender();
309
310   mFrame++;
311
312   return mRenderStatus.NeedsUpdate();
313 }
314
315 void TestApplication::ResetContext()
316 {
317   mCore->ContextDestroyed();
318   mGraphicsController.Initialize();
319   mCore->ContextCreated();
320 }
321
322 uint32_t TestApplication::Wait(uint32_t durationToWait)
323 {
324   int time = 0;
325
326   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
327   {
328     SendNotification();
329     Render(RENDER_FRAME_INTERVAL);
330     time += RENDER_FRAME_INTERVAL;
331   }
332   return time;
333 }
334
335 void TestApplication::AddScene(Integration::Scene scene)
336 {
337   mScenes.push_back(scene);
338 }
339
340 void TestApplication::RemoveScene(Integration::Scene scene)
341 {
342   mScenes.erase(std::remove(mScenes.begin(), mScenes.end(), scene), mScenes.end());
343 }
344
345 } // namespace Dali