[AT-SPI] Squashed implementation
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.cpp
1 /*
2  * Copyright (c) 2020 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 bool TestApplication::mLoggingEnabled = true;
23
24 TestApplication::TestApplication(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 TestApplication::Initialize()
45 {
46   CreateCore();
47   CreateScene();
48   InitializeCore();
49 }
50
51 void TestApplication::CreateCore()
52 {
53   // We always need the first update!
54   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
55
56   mCore = Dali::Integration::Core::New(mRenderController,
57                                        mPlatformAbstraction,
58                                        mGlAbstraction,
59                                        mGlSyncAbstraction,
60                                        mGlContextHelperAbstraction,
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
83 void TestApplication::InitializeCore()
84 {
85   mCore->SceneCreated();
86   mCore->Initialize();
87 }
88
89 TestApplication::~TestApplication()
90 {
91   Dali::Integration::Log::UninstallLogFunction();
92   delete mCore;
93 }
94
95 void TestApplication::LogContext(bool start, const char* tag)
96 {
97   if(start)
98   {
99     fprintf(stderr, "INFO: Trace Start: %s\n", tag);
100   }
101   else
102   {
103     fprintf(stderr, "INFO: Trace End: %s\n", tag);
104   }
105 }
106
107 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
108 {
109   if(mLoggingEnabled)
110   {
111     switch(level)
112     {
113       case Dali::Integration::Log::DebugInfo:
114         fprintf(stderr, "INFO: %s", message.c_str());
115         break;
116       case Dali::Integration::Log::DebugWarning:
117         fprintf(stderr, "WARN: %s", message.c_str());
118         break;
119       case Dali::Integration::Log::DebugError:
120         fprintf(stderr, "ERROR: %s", message.c_str());
121         break;
122       default:
123         fprintf(stderr, "DEFAULT: %s", message.c_str());
124         break;
125     }
126   }
127 }
128
129 Dali::Integration::Core& TestApplication::GetCore()
130 {
131   return *mCore;
132 }
133
134 TestPlatformAbstraction& TestApplication::GetPlatform()
135 {
136   return mPlatformAbstraction;
137 }
138
139 TestRenderController& TestApplication::GetRenderController()
140 {
141   return mRenderController;
142 }
143
144 TestGlAbstraction& TestApplication::GetGlAbstraction()
145 {
146   return mGlAbstraction;
147 }
148
149 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
150 {
151   return mGlSyncAbstraction;
152 }
153
154 TestGlContextHelperAbstraction& TestApplication::GetGlContextHelperAbstraction()
155 {
156   return mGlContextHelperAbstraction;
157 }
158
159 void TestApplication::ProcessEvent(const Integration::Event& event)
160 {
161   mCore->QueueEvent(event);
162   mCore->ProcessEvents();
163 }
164
165 void TestApplication::SendNotification()
166 {
167   mCore->ProcessEvents();
168 }
169
170 void TestApplication::DoUpdate(uint32_t intervalMilliseconds, const char* location)
171 {
172   if(GetUpdateStatus() == 0 &&
173      mRenderStatus.NeedsUpdate() == false &&
174      !GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc))
175   {
176     fprintf(stderr, "WARNING - Update not required :%s\n", location == NULL ? "NULL" : location);
177   }
178
179   uint32_t nextVSyncTime  = mLastVSyncTime + intervalMilliseconds;
180   float    elapsedSeconds = static_cast<float>(intervalMilliseconds) * 0.001f;
181
182   mCore->Update(elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus, false, false);
183
184   GetRenderController().Initialize();
185
186   mLastVSyncTime = nextVSyncTime;
187 }
188
189 bool TestApplication::Render(uint32_t intervalMilliseconds, const char* location)
190 {
191   DoUpdate(intervalMilliseconds, location);
192
193   // Reset the status
194   mRenderStatus.SetNeedsUpdate(false);
195   mRenderStatus.SetNeedsPostRender(false);
196
197   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/);
198   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
199   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
200   mCore->PostRender(false /*do not skip rendering*/);
201
202   mFrame++;
203
204   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
205 }
206
207 bool TestApplication::PreRenderWithPartialUpdate(uint32_t intervalMilliseconds, const char* location, std::vector<Rect<int>>& damagedRects)
208 {
209   DoUpdate(intervalMilliseconds, location);
210
211   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/);
212   mCore->PreRender(mScene, damagedRects);
213
214   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
215 }
216
217 bool TestApplication::RenderWithPartialUpdate(std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
218 {
219   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/, clippingRect);
220   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/, clippingRect);
221   mCore->PostRender(false /*do not skip rendering*/);
222
223   mFrame++;
224
225   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
226 }
227
228 uint32_t TestApplication::GetUpdateStatus()
229 {
230   return mStatus.KeepUpdating();
231 }
232
233 bool TestApplication::UpdateOnly(uint32_t intervalMilliseconds)
234 {
235   DoUpdate(intervalMilliseconds);
236   return mStatus.KeepUpdating();
237 }
238
239 bool TestApplication::GetRenderNeedsUpdate()
240 {
241   return mRenderStatus.NeedsUpdate();
242 }
243
244 bool TestApplication::GetRenderNeedsPostRender()
245 {
246   return mRenderStatus.NeedsPostRender();
247 }
248
249 bool TestApplication::RenderOnly()
250 {
251   // Update Time values
252   mCore->PreRender(mRenderStatus, false /*do not force clear*/, false /*do not skip rendering*/);
253   mCore->RenderScene(mRenderStatus, mScene, true /*render the off-screen buffers*/);
254   mCore->RenderScene(mRenderStatus, mScene, false /*render the surface*/);
255   mCore->PostRender(false /*do not skip rendering*/);
256
257   mFrame++;
258
259   return mRenderStatus.NeedsUpdate();
260 }
261
262 void TestApplication::ResetContext()
263 {
264   mCore->ContextDestroyed();
265   mGlAbstraction.Initialize();
266   mCore->ContextCreated();
267 }
268
269 uint32_t TestApplication::Wait(uint32_t durationToWait)
270 {
271   int time = 0;
272
273   for(uint32_t i = 0; i <= (durationToWait / RENDER_FRAME_INTERVAL); i++)
274   {
275     SendNotification();
276     Render(RENDER_FRAME_INTERVAL);
277     time += RENDER_FRAME_INTERVAL;
278   }
279   return time;
280 }
281
282 } // namespace Dali