66fae8f96080e9879103332003eb236885c64ac8
[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 #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
67   mCore = Dali::Integration::Core::New(mRenderController,
68                                        mPlatformAbstraction,
69                                        mGraphicsController,
70                                        Integration::RenderToFrameBuffer::FALSE,
71                                        Integration::DepthBufferAvailable::TRUE,
72                                        Integration::StencilBufferAvailable::TRUE,
73                                        mPartialUpdateEnabled ? Integration::PartialUpdateAvailable::TRUE : Integration::PartialUpdateAvailable::FALSE);
74
75   mCore->ContextCreated();
76
77   Dali::Integration::Trace::LogContext(true, "Test");
78 }
79
80 void TestGraphicsApplication::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   Graphics::RenderTargetCreateInfo createInfo{};
86   createInfo.SetSurface({nullptr})
87     .SetExtent({mSurfaceWidth, mSurfaceHeight})
88     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
89   //mRenderTarget = mGraphicsController.CreateRenderTarget(createInfo, nullptr);
90   mScene.SetSurfaceRenderTarget(createInfo);
91 }
92
93 void TestGraphicsApplication::InitializeCore()
94 {
95   mCore->SceneCreated();
96   mCore->Initialize();
97 }
98
99 TestGraphicsApplication::~TestGraphicsApplication()
100 {
101   Dali::Integration::Log::UninstallLogFunction();
102   delete mCore;
103 }
104
105 void TestGraphicsApplication::LogContext(bool start, const char* tag)
106 {
107   if(start)
108   {
109     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
110   }
111   else
112   {
113     fprintf(stderr, "INFO: Trace End: %s\n", tag);
114   }
115 }
116
117 void TestGraphicsApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
118 {
119   if(mLoggingEnabled)
120   {
121     switch(level)
122     {
123       case Dali::Integration::Log::DebugInfo:
124         fprintf(stderr, "INFO: %s", message.c_str());
125         break;
126       case Dali::Integration::Log::DebugWarning:
127         fprintf(stderr, "WARN: %s", message.c_str());
128         break;
129       case Dali::Integration::Log::DebugError:
130         fprintf(stderr, "ERROR: %s", message.c_str());
131         break;
132       default:
133         fprintf(stderr, "DEFAULT: %s", message.c_str());
134         break;
135     }
136   }
137 }
138
139 Dali::Integration::Core& TestGraphicsApplication::GetCore()
140 {
141   return *mCore;
142 }
143
144 TestPlatformAbstraction& TestGraphicsApplication::GetPlatform()
145 {
146   return mPlatformAbstraction;
147 }
148
149 TestRenderController& TestGraphicsApplication::GetRenderController()
150 {
151   return mRenderController;
152 }
153
154 Graphics::Controller& TestGraphicsApplication::GetGraphicsController()
155 {
156   return mGraphicsController;
157 }
158
159 TestGlAbstraction& TestGraphicsApplication::GetGlAbstraction()
160 {
161   return static_cast<TestGlAbstraction&>(mGraphicsController.GetGlAbstraction());
162 }
163
164 TestGlContextHelperAbstraction& TestGraphicsApplication::GetGlContextHelperAbstraction()
165 {
166   return static_cast<TestGlContextHelperAbstraction&>(mGraphicsController.GetGlContextHelperAbstraction());
167 }
168
169 void TestGraphicsApplication::ProcessEvent(const Integration::Event& event)
170 {
171   mCore->QueueEvent(event);
172   mCore->ProcessEvents();
173 }
174
175 void TestGraphicsApplication::SendNotification()
176 {
177   mCore->ProcessEvents();
178 }
179
180 void TestGraphicsApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location)
181 {
182   if(GetUpdateStatus() == 0 &&
183      mRenderStatus.NeedsUpdate() == false &&
184      !GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc))
185   {
186     fprintf(stderr, "WARNING - Update not required :%s\n", location == NULL ? "NULL" : location);
187   }
188
189   uint32_t nextVSyncTime  = mLastVSyncTime + intervalMilliseconds;
190   float    elapsedSeconds = static_cast<float>(intervalMilliseconds) * 0.001f;
191
192   mCore->Update(elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false);
193
194   GetRenderController().Initialize();
195
196   mLastVSyncTime = nextVSyncTime;
197 }
198
199 bool TestGraphicsApplication::Render(uint32_t intervalMilliseconds, const char* location)
200 {
201   DoUpdate(intervalMilliseconds, location);
202
203   // Reset the status
204   mRenderStatus.SetNeedsUpdate(false);
205   mRenderStatus.SetNeedsPostRender(false);
206
207   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/);
208   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
209   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
210   mCore->PostRender(false /*do not skip rendering*/);
211
212   mFrame++;
213
214   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
215 }
216
217 bool TestGraphicsApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
218 {
219   DoUpdate(intervalMilliseconds, location);
220
221   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/);
222   mCore->PreRender(mScene, damagedRects);
223
224   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
225 }
226
227 bool TestGraphicsApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
228 {
229   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/, clippingRect);
230   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
231   mCore->PostRender(false /*do not skip rendering*/);
232
233   mFrame++;
234
235   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
236 }
237
238 uint32_t TestGraphicsApplication::GetUpdateStatus()
239 {
240   return mStatus.KeepUpdating();
241 }
242
243 bool TestGraphicsApplication::UpdateOnly(uint32_t intervalMilliseconds)
244 {
245   DoUpdate(intervalMilliseconds);
246   return mStatus.KeepUpdating();
247 }
248
249 bool TestGraphicsApplication::GetRenderNeedsUpdate()
250 {
251   return mRenderStatus.NeedsUpdate();
252 }
253
254 bool TestGraphicsApplication::GetRenderNeedsPostRender()
255 {
256   return mRenderStatus.NeedsPostRender();
257 }
258
259 bool TestGraphicsApplication::RenderOnly()
260 {
261   // Update Time values
262   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/);
263   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
264   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
265   mCore->PostRender(false /*do not skip rendering*/);
266
267   mFrame++;
268
269   return mRenderStatus.NeedsUpdate();
270 }
271
272 void TestGraphicsApplication::ResetContext()
273 {
274   mCore->ContextDestroyed();
275   mGraphicsController.InitializeGLES(mGlAbstraction);
276   mGraphicsController.Initialize(mGraphicsSyncImplementation, mGlContextHelperAbstraction, mGraphics);
277   mCore->ContextCreated();
278 }
279
280 uint32_t TestGraphicsApplication::Wait(uint32_t durationToWait)
281 {
282   int time = 0;
283
284   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
285   {
286     SendNotification();
287     Render(RENDER_FRAME_INTERVAL);
288     time += RENDER_FRAME_INTERVAL;
289   }
290   return time;
291 }
292
293 } // namespace Dali