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