[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / dali-test-suite-utils / test-graphics-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-graphics-application.h"
19 #include <test-graphics-sync-impl.h>
20
21 namespace Dali
22 {
23 bool TestGraphicsApplication::mLoggingEnabled = true;
24
25 TestGraphicsApplication::TestGraphicsApplication(uint32_t surfaceWidth,
26                                                  uint32_t surfaceHeight,
27                                                  uint32_t horizontalDpi,
28                                                  uint32_t verticalDpi,
29                                                  bool     initialize,
30                                                  bool     enablePartialUpdate)
31 : mCore(NULL),
32   mSurfaceWidth(surfaceWidth),
33   mSurfaceHeight(surfaceHeight),
34   mFrame(0u),
35   mDpi{horizontalDpi, verticalDpi},
36   mLastVSyncTime(0u),
37   mPartialUpdateEnabled(enablePartialUpdate)
38 {
39   if(initialize)
40   {
41     Initialize();
42   }
43 }
44
45 void TestGraphicsApplication::Initialize()
46 {
47   CreateCore();
48   CreateScene();
49   InitializeCore();
50 }
51
52 void TestGraphicsApplication::CreateCore()
53 {
54   Dali::Integration::Log::LogFunction logFunction(&TestGraphicsApplication::LogMessage);
55   Dali::Integration::Log::InstallLogFunction(logFunction);
56
57   Dali::Integration::Trace::LogContextFunction logContextFunction(&TestGraphicsApplication::LogContext);
58   Dali::Integration::Trace::InstallLogContextFunction(logContextFunction);
59
60   // We always need the first update!
61   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
62
63   mGraphics.Initialize();
64   mGraphicsController.InitializeGLES(mGlAbstraction);
65   mGraphicsController.Initialize(mGraphicsSyncImplementation, mGraphics);
66   mGraphicsController.ActivateResourceContext();
67
68   Integration::CorePolicyFlags corePolicyFlags = Integration::CorePolicyFlags::DEPTH_BUFFER_AVAILABLE | Integration::CorePolicyFlags::STENCIL_BUFFER_AVAILABLE;
69   if(mPartialUpdateEnabled)
70   {
71     corePolicyFlags |= Integration::CorePolicyFlags::PARTIAL_UPDATE_AVAILABLE;
72   }
73
74   mCore = Dali::Integration::Core::New(mRenderController,
75                                        mPlatformAbstraction,
76                                        mGraphicsController,
77                                        corePolicyFlags);
78
79   mCore->ContextCreated();
80
81   Dali::Integration::Trace::LogContext(true, "Test");
82 }
83
84 void TestGraphicsApplication::CreateScene()
85 {
86   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(mSurfaceWidth), static_cast<float>(mSurfaceHeight)));
87   mScene.SetDpi(Vector2(static_cast<float>(mDpi.x), static_cast<float>(mDpi.y)));
88
89   Graphics::RenderTargetCreateInfo createInfo{};
90   createInfo.SetSurface({nullptr})
91     .SetExtent({mSurfaceWidth, mSurfaceHeight})
92     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
93   //mRenderTarget = mGraphicsController.CreateRenderTarget(createInfo, nullptr);
94   mScene.SetSurfaceRenderTarget(createInfo);
95 }
96
97 void TestGraphicsApplication::InitializeCore()
98 {
99   mCore->SceneCreated();
100   mCore->Initialize();
101 }
102
103 TestGraphicsApplication::~TestGraphicsApplication()
104 {
105   mGraphicsController.Shutdown();
106   Dali::Integration::Log::UninstallLogFunction();
107   delete mCore;
108 }
109
110 void TestGraphicsApplication::LogContext(bool start, const char* tag, const char* message)
111 {
112   if(start)
113   {
114     fprintf(stderr, "INFO: Trace Start: %s %s\n", tag, message ? message : "");
115   }
116   else
117   {
118     fprintf(stderr, "INFO: Trace End: %s %s\n", tag, message ? message : "");
119   }
120 }
121
122 void TestGraphicsApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
123 {
124   if(mLoggingEnabled)
125   {
126     switch(level)
127     {
128       case Dali::Integration::Log::DEBUG:
129         fprintf(stderr, "DEBUG: %s", message.c_str());
130         break;
131       case Dali::Integration::Log::INFO:
132         fprintf(stderr, "INFO: %s", message.c_str());
133         break;
134       case Dali::Integration::Log::WARNING:
135         fprintf(stderr, "WARN: %s", message.c_str());
136         break;
137       case Dali::Integration::Log::ERROR:
138         fprintf(stderr, "ERROR: %s", message.c_str());
139         break;
140       default:
141         fprintf(stderr, "DEFAULT: %s", message.c_str());
142         break;
143     }
144   }
145 }
146
147 Dali::Integration::Core& TestGraphicsApplication::GetCore()
148 {
149   return *mCore;
150 }
151
152 TestPlatformAbstraction& TestGraphicsApplication::GetPlatform()
153 {
154   return mPlatformAbstraction;
155 }
156
157 TestRenderController& TestGraphicsApplication::GetRenderController()
158 {
159   return mRenderController;
160 }
161
162 Graphics::Controller& TestGraphicsApplication::GetGraphicsController()
163 {
164   return mGraphicsController;
165 }
166
167 TestGlAbstraction& TestGraphicsApplication::GetGlAbstraction()
168 {
169   return static_cast<TestGlAbstraction&>(mGraphicsController.GetGlAbstraction());
170 }
171
172 void TestGraphicsApplication::ProcessEvent(const Integration::Event& event)
173 {
174   mCore->QueueEvent(event);
175   mCore->ProcessEvents();
176 }
177
178 void TestGraphicsApplication::SendNotification()
179 {
180   mCore->ProcessEvents();
181 }
182
183 void TestGraphicsApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location)
184 {
185   if(GetUpdateStatus() == 0 &&
186      mRenderStatus.NeedsUpdate() == false &&
187      !GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc))
188   {
189     fprintf(stderr, "WARNING - Update not required :%s\n", location == NULL ? "NULL" : location);
190   }
191
192   uint32_t nextVSyncTime  = mLastVSyncTime + intervalMilliseconds;
193   float    elapsedSeconds = static_cast<float>(intervalMilliseconds) * 0.001f;
194
195   mCore->Update(elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false, false);
196
197   GetRenderController().Initialize();
198
199   mLastVSyncTime = nextVSyncTime;
200 }
201
202 bool TestGraphicsApplication::Render(uint32_t intervalMilliseconds, const char* location)
203 {
204   DoUpdate(intervalMilliseconds, location);
205
206   // Reset the status
207   mRenderStatus.SetNeedsUpdate(false);
208   mRenderStatus.SetNeedsPostRender(false);
209
210   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
211   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
212   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
213   mCore->PostRender();
214
215   mFrame++;
216
217   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
218 }
219
220 bool TestGraphicsApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
221 {
222   DoUpdate(intervalMilliseconds, location);
223
224   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
225   mCore->PreRender(mScene, damagedRects);
226
227   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
228 }
229
230 bool TestGraphicsApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
231 {
232   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/, clippingRect);
233   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
234   mCore->PostRender();
235
236   mFrame++;
237
238   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
239 }
240
241 uint32_t TestGraphicsApplication::GetUpdateStatus()
242 {
243   return mStatus.KeepUpdating();
244 }
245
246 bool TestGraphicsApplication::UpdateOnly(uint32_t intervalMilliseconds)
247 {
248   DoUpdate(intervalMilliseconds);
249   return mStatus.KeepUpdating();
250 }
251
252 bool TestGraphicsApplication::GetRenderNeedsUpdate()
253 {
254   return mRenderStatus.NeedsUpdate();
255 }
256
257 bool TestGraphicsApplication::GetRenderNeedsPostRender()
258 {
259   return mRenderStatus.NeedsPostRender();
260 }
261
262 bool TestGraphicsApplication::RenderOnly()
263 {
264   // Update Time values
265   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
266   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
267   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
268   mCore->PostRender();
269
270   mFrame++;
271
272   return mRenderStatus.NeedsUpdate();
273 }
274
275 void TestGraphicsApplication::ResetContext()
276 {
277   mCore->ContextDestroyed();
278   mGraphicsController.InitializeGLES(mGlAbstraction);
279   mGraphicsController.Initialize(mGraphicsSyncImplementation, mGraphics);
280   mCore->ContextCreated();
281 }
282
283 uint32_t TestGraphicsApplication::Wait(uint32_t durationToWait)
284 {
285   int time = 0;
286
287   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
288   {
289     SendNotification();
290     Render(RENDER_FRAME_INTERVAL);
291     time += RENDER_FRAME_INTERVAL;
292   }
293   return time;
294 }
295
296 } // namespace Dali