71bc0517f8c3bc864212fc48b09b0c1dc3e9b83a
[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   Integration::CorePolicyFlags corePolicyFlags = Integration::CorePolicyFlags::DEPTH_BUFFER_AVAILABLE | Integration::CorePolicyFlags::STENCIL_BUFFER_AVAILABLE;
59   if(mPartialUpdateEnabled)
60   {
61     corePolicyFlags |= Integration::CorePolicyFlags::PARTIAL_UPDATE_AVAILABLE;
62   }
63
64   mCore = Dali::Integration::Core::New(mRenderController,
65                                        mPlatformAbstraction,
66                                        mGraphicsController,
67                                        corePolicyFlags);
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   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(mSurfaceWidth), static_cast<float>(mSurfaceHeight)));
83   mScene.SetDpi(Vector2(static_cast<float>(mDpi.x), static_cast<float>(mDpi.y)));
84
85   // Create render target for the scene
86   Graphics::RenderTargetCreateInfo rtInfo{};
87   rtInfo.SetExtent({mSurfaceWidth, mSurfaceHeight});
88   rtInfo.SetSurface(&mSurfaceWidth); // Can point to anything, really.
89
90   mScene.SetSurfaceRenderTarget(rtInfo);
91
92   mScenes.push_back(mScene);
93 }
94
95 void TestApplication::InitializeCore()
96 {
97   mCore->SceneCreated();
98   mCore->Initialize();
99 }
100
101 TestApplication::~TestApplication()
102 {
103   Dali::Integration::Log::UninstallLogFunction();
104   delete mCore;
105 }
106
107 void TestApplication::LogContext(bool start, const char* tag, const char* message)
108 {
109   if(start)
110   {
111     fprintf(stderr, "INFO: Trace Start: %s %s\n", tag, message ? message : "");
112   }
113   else
114   {
115     fprintf(stderr, "INFO: Trace End: %s %s\n", tag, message ? message : "");
116   }
117 }
118
119 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
120 {
121   if(mLoggingEnabled)
122   {
123     switch(level)
124     {
125       case Dali::Integration::Log::DEBUG:
126         fprintf(stderr, "DEBUG: %s", message.c_str());
127         break;
128       case Dali::Integration::Log::INFO:
129         fprintf(stderr, "INFO: %s", message.c_str());
130         break;
131       case Dali::Integration::Log::WARNING:
132         fprintf(stderr, "WARN: %s", message.c_str());
133         break;
134       case Dali::Integration::Log::ERROR:
135         fprintf(stderr, "ERROR: %s", message.c_str());
136         break;
137       default:
138         fprintf(stderr, "DEFAULT: %s", message.c_str());
139         break;
140     }
141   }
142 }
143
144 Dali::Integration::Core& TestApplication::GetCore()
145 {
146   return *mCore;
147 }
148
149 TestPlatformAbstraction& TestApplication::GetPlatform()
150 {
151   return mPlatformAbstraction;
152 }
153
154 TestRenderController& TestApplication::GetRenderController()
155 {
156   return mRenderController;
157 }
158
159 TestGraphicsController& TestApplication::GetGraphicsController()
160 {
161   return mGraphicsController;
162 }
163
164 TestGlAbstraction& TestApplication::GetGlAbstraction()
165 {
166   return static_cast<TestGlAbstraction&>(mGraphicsController.GetGlAbstraction());
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     for(auto&& scene : mScenes)
216     {
217       mCore->RenderScene(mRenderStatus, scene, true /*render the off-screen buffers*/);
218       mCore->RenderScene(mRenderStatus, scene, false /*render the surface*/);
219     }
220   }
221   mCore->PostRender();
222
223   mFrame++;
224
225   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
226 }
227
228 bool TestApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
229 {
230   DoUpdate(intervalMilliseconds, location);
231
232   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
233   mCore->PreRender(mScene, damagedRects);
234
235   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
236 }
237
238 bool TestApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
239 {
240   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
241   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
242   mCore->PostRender();
243
244   mFrame++;
245
246   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
247 }
248
249 bool TestApplication::RenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location)
250 {
251   DoUpdate(intervalMilliseconds, location);
252
253   // Reset the status
254   mRenderStatus.SetNeedsUpdate(false);
255   mRenderStatus.SetNeedsPostRender(false);
256
257   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
258
259   for(auto&& scene : mScenes)
260   {
261     std::vector<Rect<int>> damagedRects;
262     Rect<int>              clippingRect{};
263
264     mCore->PreRender(scene, damagedRects);
265     mCore->RenderScene(mRenderStatus, scene, true /*render the off-screen buffers*/);
266     for(auto&& iter : damagedRects)
267     {
268       clippingRect.Merge(iter);
269     }
270     mCore->RenderScene(mRenderStatus, scene, false /*render the surface*/, clippingRect);
271   }
272   mCore->PostRender();
273
274   mFrame++;
275
276   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
277 }
278
279 uint32_t TestApplication::GetUpdateStatus()
280 {
281   return mStatus.KeepUpdating();
282 }
283
284 bool TestApplication::UpdateOnly(uint32_t intervalMilliseconds)
285 {
286   DoUpdate(intervalMilliseconds);
287   return mStatus.KeepUpdating();
288 }
289
290 bool TestApplication::GetRenderNeedsUpdate()
291 {
292   return mRenderStatus.NeedsUpdate();
293 }
294
295 bool TestApplication::GetRenderNeedsPostRender()
296 {
297   return mRenderStatus.NeedsPostRender();
298 }
299
300 bool TestApplication::RenderOnly()
301 {
302   // Update Time values
303   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
304   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
305   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
306   mCore->PostRender();
307
308   mFrame++;
309
310   return mRenderStatus.NeedsUpdate();
311 }
312
313 void TestApplication::ResetContext()
314 {
315   mCore->ContextDestroyed();
316   mGraphicsController.Initialize();
317   mCore->ContextCreated();
318 }
319
320 uint32_t TestApplication::Wait(uint32_t durationToWait)
321 {
322   int time = 0;
323
324   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
325   {
326     SendNotification();
327     Render(RENDER_FRAME_INTERVAL);
328     time += RENDER_FRAME_INTERVAL;
329   }
330   return time;
331 }
332
333 void TestApplication::AddScene(Integration::Scene scene)
334 {
335   mScenes.push_back(scene);
336 }
337
338 void TestApplication::RemoveScene(Integration::Scene scene)
339 {
340   mScenes.erase(std::remove(mScenes.begin(), mScenes.end(), scene), mScenes.end());
341 }
342
343 } // namespace Dali