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