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