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