Merge "Change trace tag for text dorelayout" into devel/master
[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/integration-api/adaptor-framework/scene-holder.h>
25 #include <dali/public-api/actors/camera-actor.h>
26 #include <dali/public-api/actors/layer.h>
27 #include <dali/public-api/adaptor-framework/window.h>
28 #include <dali/public-api/animation/animation.h>
29 #include <dali/public-api/object/weak-handle.h>
30 #include <dali/public-api/render-tasks/render-task.h>
31 #include <dali/public-api/rendering/frame-buffer.h>
32 #include <dali/public-api/rendering/texture.h>
33
34 // INTERNAL INCLUDES
35 #include <dali-scene3d/internal/common/environment-map-load-task.h>
36 #include <dali-scene3d/internal/common/light-observer.h>
37 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
38
39 namespace Dali
40 {
41 namespace Scene3D
42 {
43 class SceneView;
44
45 namespace Internal
46 {
47 /**
48  * @brief Impl class for SceneView.
49  */
50 class SceneView : public Dali::Toolkit::Internal::Control
51 {
52 public:
53   /**
54    * @brief Creates a new SceneView.
55    *
56    * @return A public handle to the newly allocated SceneView.
57    */
58   static Dali::Scene3D::SceneView New();
59
60   /**
61    * @copydoc SceneView::AddCamera()
62    */
63   void AddCamera(Dali::CameraActor camera);
64
65   /**
66    * @copydoc SceneView::RemoveCamera()
67    */
68   void RemoveCamera(CameraActor camera);
69
70   /**
71    * @copydoc SceneView::GetCameraCount()
72    */
73   uint32_t GetCameraCount() const;
74
75   /**
76    * @copydoc SceneView::GetSelectedCamera()
77    */
78   CameraActor GetSelectedCamera() const;
79
80   /**
81    * @copydoc SceneView::GetCamera()
82    */
83   CameraActor GetCamera(uint32_t index) const;
84
85   /**
86    * @copydoc SceneView::GetCamera()
87    */
88   CameraActor GetCamera(const std::string& name) const;
89
90   /**
91    * @copydoc SceneView::SelectCamera()
92    */
93   void SelectCamera(uint32_t index);
94
95   /**
96    * @copydoc SceneView::SelectCamera()
97    */
98   void SelectCamera(const std::string& name);
99
100   /**
101    * @brief Register an item.
102    *
103    * Some works(e.g, lighting) of SceneView should be propagated to the child 3D items.
104    * SceneView can avoid unnecessary tree traversal to find 3D items by storing only 3D items as a list.
105    *
106    * @param[in] item scene observer to be registered.
107    */
108   void RegisterSceneItem(Scene3D::Internal::LightObserver* item);
109
110   /**
111    * @brief Unregister an item
112    *
113    * @param[in] item scene observer to be unregistered.
114    */
115   void UnregisterSceneItem(Scene3D::Internal::LightObserver* item);
116
117   /**
118    * @copydoc SceneView::SetImageBasedLightSource()
119    */
120   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
121
122   /**
123    * @copydoc SceneView::SetImageBasedLightScaleFactor()
124    */
125   void SetImageBasedLightScaleFactor(float scaleFactor);
126
127   /**
128    * @copydoc SceneView::GetImageBasedLightScaleFactor()
129    */
130   float GetImageBasedLightScaleFactor() const;
131
132   /**
133    * @brief Adds a Light object to this SceneView.
134    * Multiple light object can be added on this SceneView
135    * But the number of lights those actually turned on has limitation.
136    * Scene3D::Light::GetMaximumEnabledLightCount() method can be used to know the maximum namber.
137    *
138    * @param[in] light Light object to be added.
139    */
140   void AddLight(Scene3D::Light light);
141
142   /**
143    * @brief Removes a Light object to this SceneView.
144    * If the light is currently turned on, and SceneView has lights more than maximum number of enabled light.
145    * One of light in queue is turned on after this light is removed.
146    *
147    * @param[in] light Light object to be removed.
148    */
149   void RemoveLight(Scene3D::Light light);
150
151   /**
152    * @copydoc SceneView::GetActivatedLightCount()
153    */
154   uint32_t GetActivatedLightCount() const;
155
156   /**
157    * @copydoc SceneView::UseFramebuffer()
158    */
159   void UseFramebuffer(bool useFramebuffer);
160
161   /**
162    * @copydoc SceneView::IsUsingFramebuffer()
163    */
164   bool IsUsingFramebuffer() const;
165
166   /**
167    * @copydoc SceneView::SetFramebufferMultiSamplingLevel()
168    */
169   void SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel);
170
171   /**
172    * @copydoc SceneView::GetFramebufferMultiSamplingLevel()
173    */
174   uint8_t GetFramebufferMultiSamplingLevel() const;
175
176   /**
177    * @copydoc SceneView::SetSkybox()
178    */
179   void SetSkybox(const std::string& skyboxUrl);
180
181   /**
182    * @copydoc SceneView::SetSkyboxEnvironmentMapType()
183    */
184   void SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
185
186   /**
187    * @copydoc SceneView::SetSkyboxIntensity()
188    */
189   void SetSkyboxIntensity(float intensity);
190
191   /**
192    * @copydoc SceneView::GetSkyboxIntensity()
193    */
194   float GetSkyboxIntensity() const;
195
196   /**
197    * @copydoc SceneView::SetSkyboxOrientation()
198    */
199   void SetSkyboxOrientation(const Quaternion& orientation);
200
201   /**
202    * @copydoc SceneView::GetSkyboxOrientation()
203    */
204   Quaternion GetSkyboxOrientation() const;
205
206 protected:
207   /**
208    * @brief Constructs a new SceneView.
209    */
210   SceneView();
211
212   /**
213    * A reference counted object may only be deleted by calling Unreference()
214    */
215   virtual ~SceneView();
216
217 private:
218   /**
219    * @copydoc CustomActorImpl::OnSceneConnection()
220    */
221   void OnSceneConnection(int depth) override;
222
223   /**
224    * @copydoc CustomActorImpl::OnSceneDisconnection()
225    */
226   void OnSceneDisconnection() override;
227
228   /**
229    * @copydoc Toolkit::Control::OnInitialize()
230    */
231   void OnInitialize() override;
232
233   /**
234    * @copydoc Toolkit::Control::OnChildAdd()
235    */
236   void OnChildAdd(Actor& child) override;
237
238   /**
239    * @copydoc Toolkit::Control::OnChildRemove()
240    */
241   void OnChildRemove(Actor& child) override;
242
243   /**
244    * @copydoc Toolkit::Control::GetHeightForWidth()
245    */
246   float GetHeightForWidth(float width) override;
247
248   /**
249    * @copydoc Toolkit::Control::GetWidthForHeight()
250    */
251   float GetWidthForHeight(float height) override;
252
253   /**
254    * @copydoc Toolkit::Control::OnRelayout()
255    */
256   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
257
258   /**
259    * @copydoc Toolkit::Control::IsResourceReady()
260    */
261   bool IsResourceReady() const override;
262
263   /**
264    * @brief Changes main camera as a input camera
265    *
266    * @param camera CameraActor that will be a main camera of the SceneView
267    */
268   void UpdateCamera(CameraActor camera);
269
270   /**
271    * @brief Updates RenderTask to use selected camera and to make framebuffer
272    */
273   void UpdateRenderTask();
274
275   /**
276    * @brief Callback that will be called when window is resized.
277    */
278   void OnWindowResized(Window window, Window::WindowSize size);
279
280   /**
281    * @brief Updates camera's projection orientation according to the screen orientation.
282    */
283   void RotateCamera();
284
285   /**
286    * @brief UpdateSkybox with skybox url and skybox environment map type.
287    *
288    * @param[in] skyboxUrl image url for skybox.
289    * @param[in] skyboxEnvironmentMapType The environment type of skybox.
290    */
291   void UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
292
293   /**
294    * @brief Asynchronously skybox loading finished.
295    */
296   void OnSkyboxLoadComplete();
297
298   /**
299    * @brief Asynchronously ibl diffusel image loading finished.
300    */
301   void OnIblDiffuseLoadComplete();
302
303   /**
304    * @brief Asynchronously ibl specular image loading finished.
305    */
306   void OnIblSpecularLoadComplete();
307
308   /**
309    * @brief Asynchronously ibl loading finished.
310    */
311   void OnIblLoadComplete();
312
313   /**
314    * @brief Notify the changes of Ibl textures to the child items.
315    */
316   void NotifyImageBasedLightTextureChange();
317
318   /**
319    * @brief Internal method to add an Light object to this SceneView.
320    *
321    * @param[in] light Light object to be added.
322    * @return True if successed to enable.
323    */
324   bool AddLightInternal(Scene3D::Light light);
325
326   /**
327    * @brief Internal method to remove an Light object to this SceneView.
328    *
329    * @param[in] light Light object to be added.
330    * @return Index of removed light in enabled light list. If failed to remove it returns negative value;
331    */
332   int32_t RemoveLightInternal(Scene3D::Light light);
333
334 private:
335   Toolkit::Visual::Base mVisual;
336
337   /////////////////////////////////////////////////////////////
338   // FrameBuffer and Rendertask to render child objects as a 3D Scene
339   Dali::WeakHandle<Dali::Window>                 mWindow;
340   Integration::SceneHolder                       mSceneHolder;
341   CameraActor                                    mDefaultCamera;
342   CameraActor                                    mSelectedCamera;
343   std::vector<CameraActor>                       mCameras;
344   std::vector<Scene3D::Internal::LightObserver*> mItems;
345   Dali::FrameBuffer                              mFrameBuffer;
346   Dali::Texture                                  mTexture;
347   Dali::RenderTask                               mRenderTask;
348   Layer                                          mRootLayer;
349   int32_t                                        mWindowOrientation;
350   Dali::Actor                                    mSkybox;
351   Quaternion                                     mSkyboxOrientation;
352   float                                          mSkyboxIntensity{1.0f};
353   uint8_t                                        mFrameBufferMultiSamplingLevel{0u};
354
355   // Light
356   std::vector<std::pair<Scene3D::Light, bool>> mLights; // Pair of Light object and flag that denotes the light is currently activated or not.
357   std::vector<Scene3D::Light>                  mActivatedLights;
358   uint32_t                                     mActivatedLightCount{0u};
359
360   // Asynchronous Loading.
361   EnvironmentMapLoadTaskPtr mSkyboxLoadTask;
362   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
363   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
364   std::string               mSkyboxUrl;
365   std::string               mDiffuseIblUrl;
366   std::string               mSpecularIblUrl;
367
368   Scene3D::EnvironmentMapType mSkyboxEnvironmentMapType{Scene3D::EnvironmentMapType::AUTO};
369   Dali::Texture               mSkyboxTexture;
370   Dali::Texture               mDiffuseTexture;
371   Dali::Texture               mSpecularTexture;
372   float                       mIblScaleFactor{1.0f};
373   uint32_t                    mSpecularMipmapLevels{1u};
374   bool                        mUseFrameBuffer{false};
375   bool                        mSkyboxResourceReady{true};
376   bool                        mIblDiffuseResourceReady{true};
377   bool                        mIblSpecularResourceReady{true};
378   bool                        mSkyboxDirty{false};
379   bool                        mIblDiffuseDirty{false};
380   bool                        mIblSpecularDirty{false};
381
382   // TODO : Light Source
383 };
384
385 } // namespace Internal
386
387 // Helpers for public-api forwarding methods
388 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
389 {
390   DALI_ASSERT_ALWAYS(obj);
391   Dali::RefObject& handle = obj.GetImplementation();
392   return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
393 }
394
395 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
396 {
397   DALI_ASSERT_ALWAYS(obj);
398   const Dali::RefObject& handle = obj.GetImplementation();
399   return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
400 }
401
402 } // namespace Scene3D
403
404 } // namespace Dali
405
406 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H