a7e50383bd1301dba0648396a68f02d249334e67
[platform/core/uifw/dali-core.git] / dali / internal / event / render-tasks / render-task-list-impl.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 // CLASS HEADER
19 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/internal/common/event-to-update.h>
24 #include <dali/internal/event/common/thread-local-storage.h>
25 #include <dali/internal/event/render-tasks/render-task-defaults.h>
26 #include <dali/internal/event/render-tasks/render-task-impl.h>
27 #include <dali/internal/event/actors/camera-actor-impl.h>
28 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
29 #include <dali/internal/update/render-tasks/scene-graph-render-task-list.h>
30 #include <dali/internal/update/manager/update-manager.h>
31
32 using Dali::Internal::SceneGraph::UpdateManager;
33
34 #if defined(DEBUG_ENABLED)
35 namespace
36 {
37 Debug::Filter* gLogRenderList = Debug::Filter::New(Debug::Concise, false, "LOG_RENDER_TASK_LIST");
38 }
39 #endif
40
41 namespace Dali
42 {
43 namespace Internal
44 {
45
46 RenderTaskList* RenderTaskList::New( UpdateManager& updateManager, RenderTaskDefaults& defaults, bool systemLevel )
47 {
48   RenderTaskList* taskList = new RenderTaskList( updateManager.GetEventToUpdate(), defaults, systemLevel );
49
50   taskList->Initialize( updateManager );
51
52   return taskList;
53 }
54
55 Dali::RenderTask RenderTaskList::CreateTask()
56 {
57   RenderTask* taskImpl = RenderTask::New( mIsSystemLevel );
58
59   Dali::RenderTask newTask( taskImpl );
60   mTasks.push_back( newTask );
61
62   if ( mSceneObject )
63   {
64     SceneGraph::RenderTask* sceneObject = taskImpl->CreateSceneObject();
65     DALI_ASSERT_DEBUG( NULL != sceneObject );
66
67     // Pass ownership to SceneGraph::RenderTaskList
68     AddTaskMessage( mEventToUpdate, *mSceneObject, *sceneObject );
69   }
70
71   // Set the default source & camera actors
72   taskImpl->SetSourceActor( &mDefaults.GetDefaultRootActor() );
73   taskImpl->SetCameraActor( &mDefaults.GetDefaultCameraActor() );
74
75   return newTask;
76 }
77
78 void RenderTaskList::RemoveTask( Dali::RenderTask task )
79 {
80   for ( RenderTaskContainer::iterator iter = mTasks.begin(); mTasks.end() != iter; ++iter )
81   {
82     if ( *iter == task )
83     {
84       if ( mSceneObject )
85       {
86         RenderTask& taskImpl = GetImplementation( task );
87
88         SceneGraph::RenderTask* sceneObject = taskImpl.GetRenderTaskSceneObject();
89         DALI_ASSERT_DEBUG( NULL != sceneObject );
90
91         // Send a message to remove the scene-graph RenderTask
92         RemoveTaskMessage( mEventToUpdate, *mSceneObject, *sceneObject );
93
94         // The scene-graph RenderTask will be destroyed soon; discard the raw-pointer
95         taskImpl.DiscardSceneObject();
96       }
97
98       mTasks.erase( iter );
99       break; // we're finished
100     }
101   }
102 }
103
104 unsigned int RenderTaskList::GetTaskCount() const
105 {
106   return mTasks.size();
107 }
108
109 Dali::RenderTask RenderTaskList::GetTask( unsigned int index ) const
110 {
111   DALI_ASSERT_ALWAYS( ( index < mTasks.size() ) && "RenderTask index out-of-range" );
112
113   return mTasks[index];
114 }
115
116 void RenderTaskList::NotifyFinished()
117 {
118   DALI_LOG_TRACE_METHOD(gLogRenderList);
119
120   std::vector< Dali::RenderTask > finishedRenderTasks;
121
122   // Since render tasks can be unreferenced during the signal emissions, iterators into render tasks pointers may be invalidated.
123   // First copy the finished render tasks, then emit signals
124   for ( std::vector<Dali::RenderTask>::iterator it = mTasks.begin(), endIt = mTasks.end(); it != endIt; ++it )
125   {
126     Dali::RenderTask& renderTask( *it );
127
128     if( GetImplementation( renderTask ).HasFinished() )
129     {
130       finishedRenderTasks.push_back( Dali::RenderTask( renderTask ) );
131     }
132   }
133
134   // Now it's safe to emit the signals
135   for ( std::vector<Dali::RenderTask>::iterator it = finishedRenderTasks.begin(), endIt = finishedRenderTasks.end(); it != endIt; ++it )
136   {
137     Dali::RenderTask& handle( *it );
138
139     GetImplementation(handle).EmitSignalFinish();
140   }
141 }
142
143 RenderTaskList::RenderTaskList( EventToUpdate& eventToUpdate, RenderTaskDefaults& defaults, bool systemLevel )
144 : mEventToUpdate( eventToUpdate ),
145   mDefaults( defaults ),
146   mIsSystemLevel( systemLevel ),
147   mSceneObject( NULL )
148 {
149 }
150
151 RenderTaskList::~RenderTaskList()
152 {
153 }
154
155 void RenderTaskList::Initialize( UpdateManager& updateManager )
156 {
157   // This should only be called once, with no existing scene-object
158   DALI_ASSERT_DEBUG( NULL == mSceneObject );
159
160   // Get raw-pointer to render task list
161   mSceneObject = updateManager.GetRenderTaskList( mIsSystemLevel );
162 }
163
164 } // namespace Internal
165
166 } // namespace Dali