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