Merge "Allow worker thread trace + Trace during image works" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-adaptor.cpp
1 /*
2  * Copyright (c) 2023 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 <algorithm>
19
20 #include <toolkit-window-impl.h>
21
22 // Don't want to include the actual window.h which otherwise will be indirectly included by adaptor.h.
23 #define DALI_WINDOW_H
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/adaptor-framework/scene-holder.h>
26
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/scene.h>
29 #include <test-application.h>
30 #include <toolkit-adaptor-impl.h>
31 #include <toolkit-scene-holder-impl.h>
32 #include <toolkit-test-application.h>
33
34 namespace Dali
35 {
36 namespace Internal
37 {
38 namespace Adaptor
39 {
40 ///////////////////////////////////////////////////////////////////////////////
41 //
42 // Dali::Internal::Adaptor::Adaptor Stub
43 //
44 ///////////////////////////////////////////////////////////////////////////////
45
46 Dali::Adaptor* gAdaptor = nullptr;
47
48 Dali::Adaptor& Adaptor::New()
49 {
50   DALI_ASSERT_ALWAYS(!gAdaptor);
51   gAdaptor = new Dali::Adaptor;
52   return *gAdaptor;
53 }
54
55 Dali::Adaptor& Adaptor::Get()
56 {
57   DALI_ASSERT_ALWAYS(gAdaptor);
58   return *gAdaptor;
59 }
60
61 Adaptor::Adaptor()
62 {
63 }
64
65 Adaptor::~Adaptor()
66 {
67   gAdaptor = nullptr;
68 }
69
70 void Adaptor::Start(Dali::Window window)
71 {
72   AddWindow(&GetImplementation(window));
73 }
74
75 Integration::Scene Adaptor::GetScene(Dali::Window window)
76 {
77   return window.GetScene();
78 }
79
80 bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
81 {
82   mCallbacks.PushBack(callback);
83   return true;
84 }
85
86 void Adaptor::RemoveIdle(CallbackBase* callback)
87 {
88   mCallbacks.Erase(std::find_if(mCallbacks.Begin(), mCallbacks.End(), [&callback](CallbackBase* current) { return callback == current; }));
89 }
90
91 void Adaptor::RunIdles()
92 {
93   for(auto& callback : mCallbacks)
94   {
95     CallbackBase::Execute(*callback);
96   }
97
98   mCallbacks.Clear();
99 }
100
101 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
102 {
103   DALI_ASSERT_ALWAYS(!mWindows.empty());
104
105   return reinterpret_cast<Dali::RenderSurfaceInterface&>(mWindows.front()->GetRenderSurface());
106 }
107
108 Dali::WindowContainer Adaptor::GetWindows()
109 {
110   Dali::WindowContainer windows;
111
112   for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
113   {
114     // Downcast to Dali::Window
115     Dali::Window window(dynamic_cast<Dali::Internal::Adaptor::Window*>(*iter));
116     if(window)
117     {
118       windows.push_back(window);
119     }
120   }
121
122   return windows;
123 }
124
125 Dali::SceneHolderList Adaptor::GetSceneHolders()
126 {
127   Dali::SceneHolderList sceneHolderList;
128
129   for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
130   {
131     sceneHolderList.push_back(Dali::Integration::SceneHolder(*iter));
132   }
133
134   return sceneHolderList;
135 }
136
137 Dali::Internal::Adaptor::SceneHolder* Adaptor::GetWindow(Dali::Actor& actor)
138 {
139   Dali::Integration::Scene scene = Dali::Integration::Scene::Get(actor);
140
141   for(auto window : mWindows)
142   {
143     if(scene == window->GetScene())
144     {
145       return window;
146     }
147   }
148
149   return nullptr;
150 }
151
152 void Adaptor::AddWindow(Internal::Adaptor::SceneHolder* window)
153 {
154   if(window)
155   {
156     mWindows.push_back(window);
157
158     Dali::Integration::SceneHolder newWindow(window);
159     mWindowCreatedSignal.Emit(newWindow);
160   }
161 }
162
163 void Adaptor::RemoveWindow(Internal::Adaptor::SceneHolder* window)
164 {
165   auto iter = std::find(mWindows.begin(), mWindows.end(), window);
166   if(iter != mWindows.end())
167   {
168     mWindows.erase(iter);
169   }
170 }
171
172 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
173 {
174   Integration::Core& core = mTestApplication->GetCore();
175   core.RegisterProcessor(processor, postProcessor);
176 }
177
178 void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
179 {
180   Integration::Core& core = mTestApplication->GetCore();
181   core.UnregisterProcessor(processor, postProcessor);
182 }
183
184 void Adaptor::SetApplication(Dali::TestApplication& testApplication)
185 {
186   mTestApplication = &testApplication;
187 }
188
189 Dali::Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
190 {
191   return mResizedSignal;
192 }
193
194 Dali::Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
195 {
196   return mLanguageChangedSignal;
197 }
198
199 Dali::Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
200 {
201   return mWindowCreatedSignal;
202 }
203
204 } // namespace Adaptor
205 } // namespace Internal
206
207 ///////////////////////////////////////////////////////////////////////////////
208 //
209 // Dali::Adaptor Stub
210 //
211 ///////////////////////////////////////////////////////////////////////////////
212
213 Adaptor::Adaptor()
214 : mImpl(new Internal::Adaptor::Adaptor)
215 {
216 }
217
218 Adaptor::~Adaptor()
219 {
220   Internal::Adaptor::gAdaptor = nullptr;
221   delete mImpl;
222 }
223
224 void Adaptor::Start()
225 {
226 }
227
228 void Adaptor::Pause()
229 {
230 }
231
232 void Adaptor::Resume()
233 {
234 }
235
236 void Adaptor::Stop()
237 {
238 }
239
240 bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
241 {
242   return mImpl->AddIdle(callback, hasReturnValue);
243 }
244
245 void Adaptor::RemoveIdle(CallbackBase* callback)
246 {
247   mImpl->RemoveIdle(callback);
248 }
249
250 void Adaptor::ReplaceSurface(Window window, Dali::RenderSurfaceInterface& surface)
251 {
252 }
253
254 void Adaptor::ReplaceSurface(Dali::Integration::SceneHolder window, Dali::RenderSurfaceInterface& surface)
255 {
256 }
257
258 Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
259 {
260   return mImpl->ResizedSignal();
261 }
262
263 Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
264 {
265   return mImpl->LanguageChangedSignal();
266 }
267
268 Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
269 {
270   return mImpl->WindowCreatedSignal();
271 }
272
273 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
274 {
275   return mImpl->GetSurface();
276 }
277
278 Dali::WindowContainer Adaptor::GetWindows() const
279 {
280   return mImpl->GetWindows();
281 }
282
283 Dali::SceneHolderList Adaptor::GetSceneHolders() const
284 {
285   return mImpl->GetSceneHolders();
286 }
287
288 Any Adaptor::GetNativeWindowHandle()
289 {
290   Any window;
291   return window;
292 }
293
294 Any Adaptor::GetNativeWindowHandle(Actor actor)
295 {
296   return GetNativeWindowHandle();
297 }
298
299 void Adaptor::ReleaseSurfaceLock()
300 {
301 }
302
303 void Adaptor::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender)
304 {
305 }
306
307 Adaptor& Adaptor::Get()
308 {
309   return Internal::Adaptor::Adaptor::Get();
310 }
311
312 bool Adaptor::IsAvailable()
313 {
314   return Internal::Adaptor::gAdaptor;
315 }
316
317 void Adaptor::NotifySceneCreated()
318 {
319 }
320
321 void Adaptor::NotifyLanguageChanged()
322 {
323 }
324
325 void Adaptor::FeedTouchPoint(TouchPoint& point, int timeStamp)
326 {
327 }
328
329 void Adaptor::FeedWheelEvent(WheelEvent& wheelEvent)
330 {
331 }
332
333 void Adaptor::FeedKeyEvent(KeyEvent& keyEvent)
334 {
335 }
336
337 void Adaptor::SceneCreated()
338 {
339 }
340
341 class LogFactory : public LogFactoryInterface
342 {
343 public:
344   virtual void InstallLogFunction() const
345   {
346     Dali::Integration::Log::LogFunction logFunction(&ToolkitTestApplication::LogMessage);
347     Dali::Integration::Log::InstallLogFunction(logFunction);
348   }
349
350   LogFactory()
351   {
352   }
353   virtual ~LogFactory()
354   {
355   }
356 };
357
358 LogFactory*                gLogFactory = NULL;
359 const LogFactoryInterface& Adaptor::GetLogFactory()
360 {
361   if(gLogFactory == NULL)
362   {
363     gLogFactory = new LogFactory;
364   }
365   return *gLogFactory;
366 }
367
368 class TraceFactory : public TraceFactoryInterface
369 {
370 public:
371   virtual void InstallTraceFunction() const
372   {
373     Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
374     Dali::Integration::Trace::InstallLogContextFunction(logContextFunction);
375   }
376
377   TraceFactory()
378   {
379   }
380   virtual ~TraceFactory()
381   {
382   }
383 };
384
385 TraceFactory*                gTraceFactory = NULL;
386 const TraceFactoryInterface& Adaptor::GetTraceFactory()
387 {
388   if(gTraceFactory == NULL)
389   {
390     gTraceFactory = new TraceFactory;
391   }
392   return *gTraceFactory;
393 }
394
395 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
396 {
397   mImpl->RegisterProcessor(processor, postProcessor);
398 }
399
400 void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
401 {
402   mImpl->UnregisterProcessor(processor, postProcessor);
403 }
404
405 } // namespace Dali