Add ResourceReady for Control
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.cpp
1 /*
2  * Copyright (c) 2022 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-scene3d/internal/controls/scene-view/scene-view-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/control-devel.h>
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
26 #include <dali-toolkit/public-api/image-loader/image-url.h>
27 #include <dali-toolkit/public-api/image-loader/image.h>
28 #include <dali/devel-api/common/stage.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/object/type-registry-helper.h>
31 #include <dali/public-api/object/type-registry.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-scene3d/internal/controls/model-view/model-view-impl.h>
35 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
36
37 #include <dali/integration-api/debug.h>
38
39 using namespace Dali;
40
41 namespace Dali
42 {
43 namespace Scene3D
44 {
45 namespace Internal
46 {
47 namespace
48 {
49 BaseHandle Create()
50 {
51   return Scene3D::SceneView::New();
52 }
53
54 // Setup properties, signals and actions using the type-registry.
55 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
56 DALI_TYPE_REGISTRATION_END()
57
58 Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
59
60 } // anonymous namespace
61
62 SceneView::SceneView()
63 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT))
64 {
65 }
66
67 SceneView::~SceneView() = default;
68
69 Dali::Scene3D::SceneView SceneView::New()
70 {
71   SceneView* impl = new SceneView();
72
73   Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
74
75   // Second-phase init of the implementation
76   // This can only be done after the CustomActor connection has been made...
77   impl->Initialize();
78
79   return handle;
80 }
81
82 void SceneView::AddCamera(CameraActor camera)
83 {
84   if(camera)
85   {
86     if(mCameras.empty())
87     {
88       UpdateCamera(camera);
89     }
90     mCameras.push_back(camera);
91   }
92 }
93
94 void SceneView::RemoveCamera(CameraActor camera)
95 {
96   if(camera == mDefaultCamera)
97   {
98     DALI_LOG_ERROR("Default Camera cannot removed.\n");
99     return;
100   }
101
102   if(camera)
103   {
104     for(uint32_t i = 0; i < mCameras.size(); ++i)
105     {
106       if(mCameras[i] == camera)
107       {
108         mCameras.erase(mCameras.begin() + i);
109         break;
110       }
111     }
112
113     if(mSelectedCamera == camera)
114     {
115       CameraActor newCurrentCamera = *mCameras.begin();
116       UpdateCamera(newCurrentCamera);
117     }
118   }
119 }
120
121 uint32_t SceneView::GetCameraCount()
122 {
123   return mCameras.size();
124 }
125
126 CameraActor SceneView::GetSelectedCamera()
127 {
128   return mSelectedCamera;
129 }
130
131 CameraActor SceneView::GetCamera(uint32_t index)
132 {
133   if(index < mCameras.size())
134   {
135     return mCameras[index];
136   }
137   DALI_LOG_ERROR("Input index is out of bounds\n");
138   return CameraActor();
139 }
140
141 CameraActor SceneView::GetCamera(const std::string& name)
142 {
143   CameraActor returnCamera;
144   for(auto&& camera : mCameras)
145   {
146     if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
147     {
148       returnCamera = camera;
149       break;
150     }
151   }
152   return returnCamera;
153 }
154
155 void SceneView::SelectCamera(uint32_t index)
156 {
157   UpdateCamera(GetCamera(index));
158 }
159
160 void SceneView::SelectCamera(const std::string& name)
161 {
162   UpdateCamera(GetCamera(name));
163 }
164
165 void SceneView::RegisterModelView(Scene3D::ModelView modelView)
166 {
167   if(modelView)
168   {
169     modelView.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
170     mModels.push_back(modelView);
171   }
172 }
173
174 void SceneView::UnregisterModelView(Scene3D::ModelView modelView)
175 {
176   if(modelView)
177   {
178     for(uint32_t i = 0; i < mModels.size(); ++i)
179     {
180       if(mModels[i] == modelView)
181       {
182         mModels.erase(mModels.begin() + i);
183         break;
184       }
185     }
186   }
187 }
188
189 void SceneView::SetImageBasedLightSource(const std::string& diffuse, const std::string& specular, float scaleFactor)
190 {
191   mIBLResourceReady = false;
192   Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(diffuse);
193   if(diffuseTexture)
194   {
195     Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(specular);
196     if(specularTexture)
197     {
198       mDiffuseTexture  = diffuseTexture;
199       mSpecularTexture = specularTexture;
200       mIblScaleFactor  = scaleFactor;
201
202       for(auto&& model : mModels)
203       {
204         if(model)
205         {
206           model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
207         }
208       }
209     }
210   }
211   mIBLResourceReady = true;
212   Control::SetResourceReady(false);
213 }
214
215 void SceneView::UseFramebuffer(bool useFramebuffer)
216 {
217   if(mUseFrameBuffer != useFramebuffer)
218   {
219     mUseFrameBuffer = useFramebuffer;
220     UpdateRenderTask();
221   }
222 }
223
224 bool SceneView::IsUsingFramebuffer()
225 {
226   return mUseFrameBuffer;
227 }
228
229 ///////////////////////////////////////////////////////////
230 //
231 // Private methods
232 //
233
234 void SceneView::OnSceneConnection(int depth)
235 {
236   UpdateRenderTask();
237
238   Control::OnSceneConnection(depth);
239 }
240
241 void SceneView::OnSceneDisconnection()
242 {
243   mModels.clear();
244   Control::OnSceneDisconnection();
245 }
246
247 void SceneView::OnInitialize()
248 {
249   Actor self = Self();
250   mRootLayer = Layer::New();
251   mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
252   mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
253   // The models in the SceneView should be have independent coordinate with DALi default coordinate.
254   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
255   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
256   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
257   self.Add(mRootLayer);
258
259   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
260   mRenderTask             = taskList.CreateTask();
261   mRenderTask.SetSourceActor(mRootLayer);
262   mRenderTask.SetExclusive(true);
263   mRenderTask.SetInputEnabled(true);
264   mRenderTask.SetCullMode(false);
265   mRenderTask.SetScreenToFrameBufferMappingActor(Self());
266
267   mDefaultCamera = Dali::CameraActor::New();
268   mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
269   mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
270   mDefaultCamera.SetNearClippingPlane(1.0f);
271   AddCamera(mDefaultCamera);
272   UpdateCamera(mDefaultCamera);
273 }
274
275 void SceneView::OnChildAdd(Actor& child)
276 {
277   if(child != mRootLayer)
278   {
279     mRootLayer.Add(child);
280   }
281   Control::OnChildAdd(child);
282 }
283
284 void SceneView::OnChildRemove(Actor& child)
285 {
286   mRootLayer.Remove(child);
287   Control::OnChildRemove(child);
288 }
289
290 float SceneView::GetHeightForWidth(float width)
291 {
292   Extents padding;
293   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
294   return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
295 }
296
297 float SceneView::GetWidthForHeight(float height)
298 {
299   Extents padding;
300   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
301   return Control::GetWidthForHeight(height) + padding.start + padding.end;
302 }
303
304 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
305 {
306   Control::OnRelayout(size, container);
307   // Change canvas size of camera actor.
308   UpdateRenderTask();
309 }
310
311 bool SceneView::IsResourceReady() const
312 {
313   return mIBLResourceReady;
314 }
315
316 void SceneView::UpdateCamera(CameraActor camera)
317 {
318   if(camera)
319   {
320     if(mSelectedCamera && mSelectedCamera.GetParent())
321     {
322       mSelectedCamera.Unparent();
323     }
324     mRootLayer.Add(camera);
325   }
326
327   mSelectedCamera = camera;
328   UpdateRenderTask();
329 }
330
331 void SceneView::UpdateRenderTask()
332 {
333   if(mRenderTask)
334   {
335     if(mSelectedCamera != mRenderTask.GetCameraActor())
336     {
337       mRenderTask.SetCameraActor(mSelectedCamera);
338     }
339
340     Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
341     const float aspectRatio = size.width / size.height;
342     mSelectedCamera.SetAspectRatio(aspectRatio);
343     const float halfHeight = mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE];
344     const float halfWidth = aspectRatio * halfHeight;
345     mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE]   = -halfWidth;
346     mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE]  = halfWidth;
347     mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE]    = halfHeight; // Top is +ve to keep consistency with orthographic values
348     mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight; // Bottom is -ve to keep consistency with orthographic values
349     if(mUseFrameBuffer)
350     {
351       Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
352       if(!currentFrameBuffer ||
353          currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
354          currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
355       {
356         mRenderTask.ResetViewportGuideActor();
357         mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
358
359         // create offscreen buffer of new size to render our child actors to
360         mTexture      = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
361         mRenderTarget = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
362         mRenderTarget.AttachColorTexture(mTexture);
363         Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mRenderTarget, 0u);
364
365         Property::Map imagePropertyMap;
366         imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
367         imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
368         // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
369         imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
370         mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
371
372         Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
373
374         mRenderTask.SetFrameBuffer(mRenderTarget);
375         mRenderTask.SetClearEnabled(true);
376         mRenderTask.SetClearColor(Color::TRANSPARENT);
377       }
378     }
379     else
380     {
381       mRenderTask.SetViewportGuideActor(Self());
382       if(mRenderTask.GetFrameBuffer())
383       {
384         FrameBuffer framebuffer;
385         mRenderTask.SetFrameBuffer(framebuffer);
386         mRenderTask.SetClearEnabled(false);
387       }
388     }
389   }
390 }
391
392 } // namespace Internal
393 } // namespace Scene3D
394 } // namespace Dali