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