Making DALi core internals typesafe using guaranteed types; uint8_t, uint32_t
[platform/core/uifw/dali-core.git] / dali / internal / event / render-tasks / render-task-list-impl.cpp
1 /*
2  * Copyright (c) 2018 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/event/common/event-thread-services.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( EventThreadServices& eventServices, RenderTaskDefaults& defaults, bool systemLevel )
47 {
48   RenderTaskList* taskList = new RenderTaskList( eventServices, defaults, systemLevel );
49
50   taskList->Initialize( eventServices.GetUpdateManager() );
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
66     OwnerPointer< SceneGraph::RenderTask > transferOwnership( sceneObject );
67     AddTaskMessage( mEventThreadServices, *mSceneObject, transferOwnership );
68   }
69
70   // Set the default source & camera actors
71   taskImpl->SetSourceActor( &mDefaults.GetDefaultRootActor() );
72   taskImpl->SetCameraActor( &mDefaults.GetDefaultCameraActor() );
73
74   return newTask;
75 }
76
77 void RenderTaskList::RemoveTask( Dali::RenderTask task )
78 {
79   for ( RenderTaskContainer::iterator iter = mTasks.begin(); mTasks.end() != iter; ++iter )
80   {
81     if ( *iter == task )
82     {
83       RenderTask& taskImpl = GetImplementation( task );
84       if ( mSceneObject )
85       {
86         SceneGraph::RenderTask* sceneObject = taskImpl.GetRenderTaskSceneObject();
87         DALI_ASSERT_DEBUG( NULL != sceneObject );
88
89         // Send a message to remove the scene-graph RenderTask
90         RemoveTaskMessage( mEventThreadServices, *mSceneObject, *sceneObject );
91
92         // The scene-graph RenderTask will be destroyed soon; discard the raw-pointer
93         taskImpl.DiscardSceneObject();
94       }
95
96       mTasks.erase( iter );
97
98       for ( Vector< Exclusive >::Iterator exclusiveIt = mExclusives.Begin(); exclusiveIt != mExclusives.End(); ++exclusiveIt )
99       {
100         if ( exclusiveIt->renderTaskPtr == &taskImpl )
101         {
102           mExclusives.Erase( exclusiveIt );
103           break;
104         }
105       }
106       break; // we're finished
107     }
108   }
109 }
110
111 uint32_t RenderTaskList::GetTaskCount() const
112 {
113   return static_cast<uint32_t>( mTasks.size() ); // only 4,294,967,295 render tasks supported
114 }
115
116 Dali::RenderTask RenderTaskList::GetTask( uint32_t index ) const
117 {
118   DALI_ASSERT_ALWAYS( ( index < mTasks.size() ) && "RenderTask index out-of-range" );
119
120   return mTasks[index];
121 }
122
123 void RenderTaskList::SetExclusive( RenderTask* task, bool exclusive )
124 {
125   // Check to see if this rendertask has an entry?
126   for ( Vector< Exclusive >::Iterator exclusiveIt = mExclusives.Begin(); exclusiveIt != mExclusives.End(); ++exclusiveIt )
127   {
128     if ( exclusiveIt->renderTaskPtr == task )
129     {
130       if ( !exclusive )
131       {
132         mExclusives.Erase( exclusiveIt );
133         break;
134       }
135       else
136       {
137         exclusiveIt->actorPtr = task->GetSourceActor();
138         exclusive = false;
139         break;
140       }
141     }
142   }
143   if ( exclusive )
144   {
145     Exclusive exclusiveSlot;
146     exclusiveSlot.renderTaskPtr = task;
147     exclusiveSlot.actorPtr = task->GetSourceActor();
148     mExclusives.PushBack( exclusiveSlot );
149   }
150 }
151
152 RenderTaskList::RenderTaskList( EventThreadServices& eventThreadServices, RenderTaskDefaults& defaults, bool systemLevel )
153 : mEventThreadServices( eventThreadServices ),
154   mDefaults( defaults ),
155   mIsSystemLevel( systemLevel ),
156   mSceneObject( NULL )
157 {
158 }
159
160 RenderTaskList::~RenderTaskList()
161 {
162 }
163
164 void RenderTaskList::Initialize( UpdateManager& updateManager )
165 {
166   // This should only be called once, with no existing scene-object
167   DALI_ASSERT_DEBUG( NULL == mSceneObject );
168
169   // Get raw-pointer to render task list
170   mSceneObject = updateManager.GetRenderTaskList( mIsSystemLevel );
171   // set the callback to call us back when tasks are completed
172   mSceneObject->SetCompleteNotificationInterface( this );
173 }
174
175 void RenderTaskList::NotifyCompleted()
176 {
177   DALI_LOG_TRACE_METHOD(gLogRenderList);
178
179   std::vector< Dali::RenderTask > finishedRenderTasks;
180
181   // Since render tasks can be unreferenced during the signal emissions, iterators into render tasks pointers may be invalidated.
182   // First copy the finished render tasks, then emit signals
183   for ( std::vector<Dali::RenderTask>::iterator it = mTasks.begin(), endIt = mTasks.end(); it != endIt; ++it )
184   {
185     Dali::RenderTask& renderTask( *it );
186
187     if( GetImplementation( renderTask ).HasFinished() )
188     {
189       finishedRenderTasks.push_back( Dali::RenderTask( renderTask ) );
190     }
191   }
192
193   // Now it's safe to emit the signals
194   for ( std::vector<Dali::RenderTask>::iterator it = finishedRenderTasks.begin(), endIt = finishedRenderTasks.end(); it != endIt; ++it )
195   {
196     Dali::RenderTask& handle( *it );
197
198     GetImplementation(handle).EmitSignalFinish();
199   }
200 }
201
202 void RenderTaskList::RecoverFromContextLoss()
203 {
204   for ( RenderTaskContainer::iterator iter = mTasks.begin(); mTasks.end() != iter; ++iter )
205   {
206     Dali::RenderTask task = *iter;
207
208     // If the render target renders only once to an offscreen, re-render the render task
209     if( task.GetRefreshRate() == Dali::RenderTask::REFRESH_ONCE && task.GetTargetFrameBuffer() )
210     {
211       task.SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
212     }
213   }
214 }
215
216 } // namespace Internal
217
218 } // namespace Dali