[dali_1.4.9] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / render-tasks / render-task-list-impl.cpp
1 /*
2  * Copyright (c) 2019 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/stage-impl.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 RenderTaskListPtr RenderTaskList::New()
47 {
48   RenderTaskListPtr taskList = new RenderTaskList();
49
50   taskList->Initialize();
51
52   return taskList;
53 }
54
55 RenderTaskPtr RenderTaskList::CreateTask()
56 {
57   return CreateTask( &mDefaults.GetDefaultRootActor(), &mDefaults.GetDefaultCameraActor() );
58 }
59
60 RenderTaskPtr RenderTaskList::CreateTask( Actor* sourceActor, CameraActor* cameraActor)
61 {
62   RenderTaskPtr task = RenderTask::New( sourceActor, cameraActor, *this );
63
64   mTasks.push_back( task );
65
66   return task;
67 }
68
69 void RenderTaskList::RemoveTask( Internal::RenderTask& task )
70 {
71   for ( RenderTaskContainer::iterator iter = mTasks.begin(); mTasks.end() != iter; ++iter )
72   {
73     if ( iter->Get() == &task )
74     {
75       const SceneGraph::RenderTask& sceneObject = task.GetRenderTaskSceneObject();
76
77       // delete the task
78       mTasks.erase( iter );
79       // send a message to remove the scene-graph RenderTask
80       RemoveTaskMessage( mEventThreadServices, *mSceneObject, sceneObject );
81
82       for ( Vector< Exclusive >::Iterator exclusiveIt = mExclusives.Begin(); exclusiveIt != mExclusives.End(); ++exclusiveIt )
83       {
84         if ( exclusiveIt->renderTaskPtr == iter->Get() )
85         {
86           mExclusives.Erase( exclusiveIt );
87           break;
88         }
89       }
90       break; // we're finished
91     }
92   }
93 }
94
95 uint32_t RenderTaskList::GetTaskCount() const
96 {
97   return static_cast<uint32_t>( mTasks.size() ); // only 4,294,967,295 render tasks supported
98 }
99
100 RenderTaskPtr RenderTaskList::GetTask( uint32_t index ) const
101 {
102   DALI_ASSERT_ALWAYS( ( index < mTasks.size() ) && "RenderTask index out-of-range" );
103
104   return mTasks[ index ];
105 }
106
107 void RenderTaskList::SetExclusive( RenderTask* task, bool exclusive )
108 {
109   // Check to see if this rendertask has an entry?
110   for ( Vector< Exclusive >::Iterator exclusiveIt = mExclusives.Begin(); exclusiveIt != mExclusives.End(); ++exclusiveIt )
111   {
112     if ( exclusiveIt->renderTaskPtr == task )
113     {
114       if ( !exclusive )
115       {
116         mExclusives.Erase( exclusiveIt );
117         break;
118       }
119       else
120       {
121         exclusiveIt->actorPtr = task->GetSourceActor();
122         exclusive = false;
123         break;
124       }
125     }
126   }
127   if ( exclusive )
128   {
129     Exclusive exclusiveSlot;
130     exclusiveSlot.renderTaskPtr = task;
131     exclusiveSlot.actorPtr = task->GetSourceActor();
132     mExclusives.PushBack( exclusiveSlot );
133   }
134 }
135
136 RenderTaskList::RenderTaskList()
137 : mEventThreadServices( EventThreadServices::Get() ),
138   mDefaults( *Stage::GetCurrent() ),
139   mSceneObject( nullptr )
140 {
141 }
142
143 RenderTaskList::~RenderTaskList()
144 {
145   if( EventThreadServices::IsCoreRunning() && mSceneObject )
146   {
147     // Remove the render task list using a message to the update manager
148     RemoveRenderTaskListMessage( mEventThreadServices.GetUpdateManager(), *mSceneObject );
149   }
150 }
151
152 void RenderTaskList::Initialize()
153 {
154   // Create a new render task list, Keep a const pointer to the render task list.
155   mSceneObject = SceneGraph::RenderTaskList::New();
156
157   OwnerPointer< SceneGraph::RenderTaskList > transferOwnership( const_cast< SceneGraph::RenderTaskList* >( mSceneObject ) );
158   AddRenderTaskListMessage( mEventThreadServices.GetUpdateManager(), transferOwnership );
159
160   // set the callback to call us back when tasks are completed
161   mSceneObject->SetCompleteNotificationInterface( this );
162 }
163
164 void RenderTaskList::NotifyCompleted()
165 {
166   DALI_LOG_TRACE_METHOD(gLogRenderList);
167
168   RenderTaskContainer finishedRenderTasks;
169
170   // Since render tasks can be unreferenced during the signal emissions, iterators into render tasks pointers may be invalidated.
171   // First copy the finished render tasks, then emit signals
172   for ( RenderTaskContainer::iterator iter = mTasks.begin(), endIt = mTasks.end(); iter != endIt; ++iter )
173   {
174     if( (*iter)->HasFinished() )
175     {
176       finishedRenderTasks.push_back( *iter );
177     }
178   }
179
180   // Now it's safe to emit the signals
181   for ( auto&& item : finishedRenderTasks )
182   {
183     item->EmitSignalFinish();
184   }
185 }
186
187 void RenderTaskList::RecoverFromContextLoss()
188 {
189   for ( auto&& item : mTasks )
190   {
191     // If the render target renders only once to an offscreen, re-render the render task
192     if( item->GetRefreshRate() == Dali::RenderTask::REFRESH_ONCE && item->GetTargetFrameBuffer() )
193     {
194       item->SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
195     }
196   }
197 }
198
199 const SceneGraph::RenderTaskList& RenderTaskList::GetSceneObject() const
200 {
201   return *mSceneObject;
202 }
203
204 } // namespace Internal
205
206 } // namespace Dali