Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / relayout-controller.cpp
1 /*
2  * Copyright (c) 2014 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 // FILE HEADER
19
20 #include "relayout-controller.h"
21 #include "relayout-controller-impl.h"
22
23 // EXTERNAL INCLUDES
24 #include <stack>
25 #include <sstream>
26 #include <dali/public-api/adaptor-framework/singleton-service.h>
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30
31 #include "dali-toolkit/public-api/controls/control.h"
32 #include "dali-toolkit/public-api/controls/control-impl.h"
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45 // Flag to avoid doing more than one request per frame
46 // getting the singleton handle can be expensive as it requires calling to adaptor, dynamic cast etc
47 // and it can get called 100 times per frame easily in startup and new view initialization
48   bool gRelayoutRequestPending = false;
49 }
50
51 RelayoutController::RelayoutController()
52 {
53
54 }
55
56 RelayoutController::~RelayoutController()
57 {
58
59 }
60
61 void RelayoutController::Request()
62 {
63   // are we already going to do it this frame
64   if( !gRelayoutRequestPending )
65   {
66     RelayoutController controller;
67
68     // Check whether the RelayoutController is already created
69     SingletonService singletonService( SingletonService::Get() );
70     if ( singletonService )
71     {
72       Dali::BaseHandle handle = singletonService.GetSingleton(typeid(RelayoutController));
73
74       if(handle)
75       {
76         // If so, downcast the handle of singleton to RelayoutController
77         controller = RelayoutController(dynamic_cast<Internal::RelayoutControllerImpl*>(handle.GetObjectPtr()));
78       }
79
80       if(!controller)
81       {
82         // If not, create the RelayoutController and register it as a singleton
83         controller = RelayoutController( new Internal::RelayoutControllerImpl(gRelayoutRequestPending) );
84         singletonService.Register( typeid(controller), controller );
85       }
86
87       GetImpl(controller).Request();
88       gRelayoutRequestPending = true;
89     }
90   }
91 }
92
93 RelayoutController::RelayoutController(Internal::RelayoutControllerImpl *impl)
94   : BaseHandle(impl)
95 {
96 }
97
98 } // namespace Internal
99
100 } // namespace Toolkit
101
102 } // namespace Dali
103