Cleaned up some of the circular dependency between GLES::Context & GLES::GraphicsCont...
[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, mGlContextHelperAbstraction, mGraphics);
66   mGraphicsController.ActivateResourceContext();
67
68   mCore = Dali::Integration::Core::New(mRenderController,
69                                        mPlatformAbstraction,
70                                        mGraphicsController,
71                                        Integration::RenderToFrameBuffer::FALSE,
72                                        Integration::DepthBufferAvailable::TRUE,
73                                        Integration::StencilBufferAvailable::TRUE,
74                                        mPartialUpdateEnabled ? Integration::PartialUpdateAvailable::TRUE : Integration::PartialUpdateAvailable::FALSE);
75
76   mCore->ContextCreated();
77
78   Dali::Integration::Trace::LogContext(true, "Test");
79 }
80
81 void TestGraphicsApplication::CreateScene()
82 {
83   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(mSurfaceWidth), static_cast<float>(mSurfaceHeight)));
84   mScene.SetDpi(Vector2(static_cast<float>(mDpi.x), static_cast<float>(mDpi.y)));
85
86   Graphics::RenderTargetCreateInfo createInfo{};
87   createInfo.SetSurface({nullptr})
88     .SetExtent({mSurfaceWidth, mSurfaceHeight})
89     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
90   //mRenderTarget = mGraphicsController.CreateRenderTarget(createInfo, nullptr);
91   mScene.SetSurfaceRenderTarget(createInfo);
92 }
93
94 void TestGraphicsApplication::InitializeCore()
95 {
96   mCore->SceneCreated();
97   mCore->Initialize();
98 }
99
100 TestGraphicsApplication::~TestGraphicsApplication()
101 {
102   mGraphicsController.Shutdown();
103   Dali::Integration::Log::UninstallLogFunction();
104   delete mCore;
105 }
106
107 void TestGraphicsApplication::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 TestGraphicsApplication::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& TestGraphicsApplication::GetCore()
145 {
146   return *mCore;
147 }
148
149 TestPlatformAbstraction& TestGraphicsApplication::GetPlatform()
150 {
151   return mPlatformAbstraction;
152 }
153
154 TestRenderController& TestGraphicsApplication::GetRenderController()
155 {
156   return mRenderController;
157 }
158
159 Graphics::Controller& TestGraphicsApplication::GetGraphicsController()
160 {
161   return mGraphicsController;
162 }
163
164 TestGlAbstraction& TestGraphicsApplication::GetGlAbstraction()
165 {
166   return static_cast<TestGlAbstraction&>(mGraphicsController.GetGlAbstraction());
167 }
168
169 TestGlContextHelperAbstraction& TestGraphicsApplication::GetGlContextHelperAbstraction()
170 {
171   return static_cast<TestGlContextHelperAbstraction&>(mGraphicsController.GetGlContextHelperAbstraction());
172 }
173
174 void TestGraphicsApplication::ProcessEvent(const Integration::Event& event)
175 {
176   mCore->QueueEvent(event);
177   mCore->ProcessEvents();
178 }
179
180 void TestGraphicsApplication::SendNotification()
181 {
182   mCore->ProcessEvents();
183 }
184
185 void TestGraphicsApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location)
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, false);
198
199   GetRenderController().Initialize();
200
201   mLastVSyncTime = nextVSyncTime;
202 }
203
204 bool TestGraphicsApplication::Render(uint32_t intervalMilliseconds, const char* location)
205 {
206   DoUpdate(intervalMilliseconds, location);
207
208   // Reset the status
209   mRenderStatus.SetNeedsUpdate(false);
210   mRenderStatus.SetNeedsPostRender(false);
211
212   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
213   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
214   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
215   mCore->PostRender();
216
217   mFrame++;
218
219   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
220 }
221
222 bool TestGraphicsApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
223 {
224   DoUpdate(intervalMilliseconds, location);
225
226   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
227   mCore->PreRender(mScene, damagedRects);
228
229   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
230 }
231
232 bool TestGraphicsApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
233 {
234   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/, clippingRect);
235   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
236   mCore->PostRender();
237
238   mFrame++;
239
240   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
241 }
242
243 uint32_t TestGraphicsApplication::GetUpdateStatus()
244 {
245   return mStatus.KeepUpdating();
246 }
247
248 bool TestGraphicsApplication::UpdateOnly(uint32_t intervalMilliseconds)
249 {
250   DoUpdate(intervalMilliseconds);
251   return mStatus.KeepUpdating();
252 }
253
254 bool TestGraphicsApplication::GetRenderNeedsUpdate()
255 {
256   return mRenderStatus.NeedsUpdate();
257 }
258
259 bool TestGraphicsApplication::GetRenderNeedsPostRender()
260 {
261   return mRenderStatus.NeedsPostRender();
262 }
263
264 bool TestGraphicsApplication::RenderOnly()
265 {
266   // Update Time values
267   mCore->PreRender(mRenderStatus, false /*do not force clear*/);
268   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
269   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
270   mCore->PostRender();
271
272   mFrame++;
273
274   return mRenderStatus.NeedsUpdate();
275 }
276
277 void TestGraphicsApplication::ResetContext()
278 {
279   mCore->ContextDestroyed();
280   mGraphicsController.InitializeGLES(mGlAbstraction);
281   mGraphicsController.Initialize(mGraphicsSyncImplementation, mGlContextHelperAbstraction, mGraphics);
282   mCore->ContextCreated();
283 }
284
285 uint32_t TestGraphicsApplication::Wait(uint32_t durationToWait)
286 {
287   int time = 0;
288
289   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
290   {
291     SendNotification();
292     Render(RENDER_FRAME_INTERVAL);
293     time += RENDER_FRAME_INTERVAL;
294   }
295   return time;
296 }
297
298 } // namespace Dali