39a98bada3a90c777b32e97f22cf0ddd3b17169c
[platform/core/uifw/dali-core.git] / dali / internal / event / render-tasks / render-task-list-impl.cpp
1 /*
2  * Copyright (c) 2021 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/internal/event/actors/camera-actor-impl.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/update/manager/update-manager.h>
28 #include <dali/internal/update/render-tasks/scene-graph-render-task-list.h>
29 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
30 #include <dali/public-api/common/dali-common.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 RenderTaskListPtr RenderTaskList::New()
46 {
47   RenderTaskListPtr taskList = new RenderTaskList();
48
49   taskList->Initialize();
50
51   return taskList;
52 }
53
54 RenderTaskPtr RenderTaskList::CreateTask()
55 {
56   return CreateTask(&mDefaults.GetDefaultRootActor(), &mDefaults.GetDefaultCameraActor());
57 }
58
59 RenderTaskPtr RenderTaskList::CreateTask(Actor* sourceActor, CameraActor* cameraActor)
60 {
61   RenderTaskPtr task = RenderTask::New(sourceActor, cameraActor, *this);
62
63   mTasks.push_back(task);
64
65   return task;
66 }
67
68 void RenderTaskList::RemoveTask(Internal::RenderTask& task)
69 {
70   for(RenderTaskContainer::iterator iter = mTasks.begin(); mTasks.end() != iter; ++iter)
71   {
72     RenderTask* ptr = iter->Get();
73
74     if(ptr == &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(auto exclusiveIt = mExclusives.begin(); exclusiveIt != mExclusives.end(); ++exclusiveIt)
84       {
85         if(exclusiveIt->renderTaskPtr == ptr)
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(auto 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->actor.SetActor(task->GetSourceActor());
123         exclusive = false;
124         break;
125       }
126     }
127   }
128   if(exclusive)
129   {
130     Exclusive exclusiveSlot;
131     exclusiveSlot.renderTaskPtr = task;
132     exclusiveSlot.actor.SetActor(task->GetSourceActor());
133     mExclusives.emplace_back(std::move(exclusiveSlot));
134   }
135 }
136
137 RenderTaskList::RenderTaskList()
138 : mEventThreadServices(EventThreadServices::Get()),
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->GetFrameBuffer())
194     {
195       item->SetRefreshRate(Dali::RenderTask::REFRESH_ONCE);
196     }
197   }
198 }
199
200 const SceneGraph::RenderTaskList& RenderTaskList::GetSceneObject() const
201 {
202   return *mSceneObject;
203 }
204
205 } // namespace Internal
206
207 } // namespace Dali