DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
2 #define DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/internal/visuals/image/image-visual.h>
23 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali/public-api/actors/camera-actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/public-api/adaptor-framework/window.h>
27 #include <dali/public-api/animation/animation.h>
28 #include <dali/public-api/object/weak-handle.h>
29 #include <dali/public-api/render-tasks/render-task.h>
30 #include <dali/public-api/rendering/frame-buffer.h>
31 #include <dali/public-api/rendering/texture.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
35 #include <dali-scene3d/internal/common/environment-map-load-task.h>
36 #include <dali-scene3d/internal/common/image-based-light-observer.h>
37
38 namespace Dali
39 {
40 namespace Scene3D
41 {
42 class SceneView;
43
44 namespace Internal
45 {
46 /**
47  * @brief Impl class for SceneView.
48  */
49 class SceneView : public Dali::Toolkit::Internal::Control
50 {
51 public:
52   /**
53    * @brief Creates a new SceneView.
54    *
55    * @return A public handle to the newly allocated SceneView.
56    */
57   static Dali::Scene3D::SceneView New();
58
59   /**
60    * @copydoc SceneView::AddCamera()
61    */
62   void AddCamera(Dali::CameraActor camera);
63
64   /**
65    * @copydoc SceneView::RemoveCamera()
66    */
67   void RemoveCamera(CameraActor camera);
68
69   /**
70    * @copydoc SceneView::GetCameraCount()
71    */
72   uint32_t GetCameraCount() const;
73
74   /**
75    * @copydoc SceneView::GetSelectedCamera()
76    */
77   CameraActor GetSelectedCamera() const;
78
79   /**
80    * @copydoc SceneView::GetCamera()
81    */
82   CameraActor GetCamera(uint32_t index) const;
83
84   /**
85    * @copydoc SceneView::GetCamera()
86    */
87   CameraActor GetCamera(const std::string& name) const;
88
89   /**
90    * @copydoc SceneView::SelectCamera()
91    */
92   void SelectCamera(uint32_t index);
93
94   /**
95    * @copydoc SceneView::SelectCamera()
96    */
97   void SelectCamera(const std::string& name);
98
99   /**
100    * @brief Register an item.
101    *
102    * Some works(e.g, lighting) of SceneView should be propagated to the child 3D items.
103    * SceneView can avoid unnecessary tree traversal to find 3D items by storing only 3D items as a list.
104    *
105    * @param[in] item scene observer to be registered.
106    */
107   void RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item);
108
109   /**
110    * @brief Unregister an item
111    *
112    * @param[in] item scene observer to be unregistered.
113    */
114   void UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item);
115
116   /**
117    * @copydoc SceneView::SetImageBasedLightSource()
118    */
119   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
120
121   /**
122    * @copydoc SceneView::SetImageBasedLightScaleFactor()
123    */
124   void SetImageBasedLightScaleFactor(float scaleFactor);
125
126   /**
127    * @copydoc SceneView::GetImageBasedLightScaleFactor()
128    */
129   float GetImageBasedLightScaleFactor() const;
130
131   /**
132    * @copydoc SceneView::UseFramebuffer()
133    */
134   void UseFramebuffer(bool useFramebuffer);
135
136   /**
137    * @copydoc SceneView::IsUsingFramebuffer()
138    */
139   bool IsUsingFramebuffer() const;
140
141   /**
142    * @copydoc SceneView::SetSkybox()
143    */
144   void SetSkybox(const std::string& skyboxUrl);
145
146   /**
147    * @copydoc SceneView::SetSkyboxEnvironmentMapType()
148    */
149   void SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
150
151   /**
152    * @copydoc SceneView::SetSkyboxIntensity()
153    */
154   void SetSkyboxIntensity(float intensity);
155
156   /**
157    * @copydoc SceneView::GetSkyboxIntensity()
158    */
159   float GetSkyboxIntensity() const;
160
161   /**
162    * @copydoc SceneView::SetSkyboxOrientation()
163    */
164   void SetSkyboxOrientation(const Quaternion& orientation);
165
166   /**
167    * @copydoc SceneView::GetSkyboxOrientation()
168    */
169   Quaternion GetSkyboxOrientation() const;
170
171 protected:
172   /**
173    * @brief Constructs a new SceneView.
174    */
175   SceneView();
176
177   /**
178    * A reference counted object may only be deleted by calling Unreference()
179    */
180   virtual ~SceneView();
181
182 private:
183   /**
184    * @copydoc CustomActorImpl::OnSceneConnection()
185    */
186   void OnSceneConnection(int depth) override;
187
188   /**
189    * @copydoc CustomActorImpl::OnSceneDisconnection()
190    */
191   void OnSceneDisconnection() override;
192
193   /**
194    * @copydoc Toolkit::Control::OnInitialize()
195    */
196   void OnInitialize() override;
197
198   /**
199    * @copydoc Toolkit::Control::OnChildAdd()
200    */
201   void OnChildAdd(Actor& child) override;
202
203   /**
204    * @copydoc Toolkit::Control::OnChildRemove()
205    */
206   void OnChildRemove(Actor& child) override;
207
208   /**
209    * @copydoc Toolkit::Control::GetHeightForWidth()
210    */
211   float GetHeightForWidth(float width) override;
212
213   /**
214    * @copydoc Toolkit::Control::GetWidthForHeight()
215    */
216   float GetWidthForHeight(float height) override;
217
218   /**
219    * @copydoc Toolkit::Control::OnRelayout()
220    */
221   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
222
223   /**
224    * @copydoc Toolkit::Control::IsResourceReady()
225    */
226   bool IsResourceReady() const override;
227
228   /**
229    * @brief Changes main camera as a input camera
230    *
231    * @param camera CameraActor that will be a main camera of the SceneView
232    */
233   void UpdateCamera(CameraActor camera);
234
235   /**
236    * @brief Updates RenderTask to use selected camera and to make framebuffer
237    */
238   void UpdateRenderTask();
239
240   /**
241    * @brief Callback that will be called when window is resized.
242    */
243   void OnWindowResized(Window window, Window::WindowSize size);
244
245   /**
246    * @brief Updates camera's projection orientation according to the screen orientation.
247    */
248   void RotateCamera();
249
250   /**
251    * @brief UpdateSkybox with skybox url and skybox environment map type.
252    *
253    * @param[in] skyboxUrl image url for skybox.
254    * @param[in] skyboxEnvironmentMapType The environment type of skybox.
255    */
256   void UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
257
258   /**
259    * @brief Asynchronously skybox loading finished.
260    */
261   void OnSkyboxLoadComplete();
262
263   /**
264    * @brief Asynchronously ibl diffusel image loading finished.
265    */
266   void OnIblDiffuseLoadComplete();
267
268   /**
269    * @brief Asynchronously ibl specular image loading finished.
270    */
271   void OnIblSpecularLoadComplete();
272
273   /**
274    * @brief Asynchronously ibl loading finished.
275    */
276   void OnIblLoadComplete();
277
278   /**
279    * @brief Notify the changes of Ibl textures to the child items.
280    */
281   void NotifyImageBasedLightTextureChange();
282
283 private:
284   Toolkit::Visual::Base mVisual;
285
286   /////////////////////////////////////////////////////////////
287   // FrameBuffer and Rendertask to render child objects as a 3D Scene
288   Dali::WeakHandle<Dali::Window>                           mWindow;
289   CameraActor                                              mDefaultCamera;
290   CameraActor                                              mSelectedCamera;
291   std::vector<CameraActor>                                 mCameras;
292   std::vector<Scene3D::Internal::ImageBasedLightObserver*> mItems;
293   Dali::FrameBuffer                                        mFrameBuffer;
294   Dali::Texture                                            mTexture;
295   Dali::RenderTask                                         mRenderTask;
296   Layer                                                    mRootLayer;
297   int32_t                                                  mWindowOrientation;
298   Dali::Actor                                              mSkybox;
299   Quaternion                                               mSkyboxOrientation;
300   float                                                    mSkyboxIntensity{1.0f};
301
302   // Asynchronous Loading.
303   EnvironmentMapLoadTaskPtr       mSkyboxLoadTask;
304   EnvironmentMapLoadTaskPtr       mIblDiffuseLoadTask;
305   EnvironmentMapLoadTaskPtr       mIblSpecularLoadTask;
306   std::string                     mSkyboxUrl;
307   std::string                     mDiffuseIblUrl;
308   std::string                     mSpecularIblUrl;
309
310   Scene3D::EnvironmentMapType mSkyboxEnvironmentMapType{Scene3D::EnvironmentMapType::AUTO};
311   Dali::Texture               mSkyboxTexture;
312   Dali::Texture               mDiffuseTexture;
313   Dali::Texture               mSpecularTexture;
314   float                       mIblScaleFactor{1.0f};
315   bool                        mUseFrameBuffer{false};
316   bool                        mSkyboxResourceReady{true};
317   bool                        mIblDiffuseResourceReady{true};
318   bool                        mIblSpecularResourceReady{true};
319   bool                        mSkyboxDirty{false};
320   bool                        mIblDiffuseDirty{false};
321   bool                        mIblSpecularDirty{false};
322
323   // TODO : Light Source
324 };
325
326 } // namespace Internal
327
328 // Helpers for public-api forwarding methods
329 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
330 {
331   DALI_ASSERT_ALWAYS(obj);
332   Dali::RefObject& handle = obj.GetImplementation();
333   return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
334 }
335
336 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
337 {
338   DALI_ASSERT_ALWAYS(obj);
339   const Dali::RefObject& handle = obj.GetImplementation();
340   return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
341 }
342
343 } // namespace Scene3D
344
345 } // namespace Dali
346
347 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H