[dali_2.3.21] Merge branch '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) 2024 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 #include <dali-scene3d/public-api/loader/shader-manager.h>
39
40 namespace Dali
41 {
42 namespace Scene3D
43 {
44 class SceneView;
45
46 namespace Internal
47 {
48 /**
49  * @brief Impl class for SceneView.
50  */
51 class SceneView : public Dali::Toolkit::Internal::Control
52 {
53 public:
54   /**
55    * @brief Creates a new SceneView.
56    *
57    * @return A public handle to the newly allocated SceneView.
58    */
59   static Dali::Scene3D::SceneView New();
60
61   /**
62    * @copydoc SceneView::AddCamera()
63    */
64   void AddCamera(Dali::CameraActor camera);
65
66   /**
67    * @copydoc SceneView::RemoveCamera()
68    */
69   void RemoveCamera(CameraActor camera);
70
71   /**
72    * @copydoc SceneView::GetCameraCount()
73    */
74   uint32_t GetCameraCount() const;
75
76   /**
77    * @copydoc SceneView::GetSelectedCamera()
78    */
79   CameraActor GetSelectedCamera() const;
80
81   /**
82    * @copydoc SceneView::GetCamera()
83    */
84   CameraActor GetCamera(uint32_t index) const;
85
86   /**
87    * @copydoc SceneView::GetCamera()
88    */
89   CameraActor GetCamera(const std::string& name) const;
90
91   /**
92    * @copydoc SceneView::SelectCamera()
93    */
94   void SelectCamera(uint32_t index);
95
96   /**
97    * @copydoc SceneView::SelectCamera()
98    */
99   void SelectCamera(const std::string& name);
100
101   /**
102    * @brief Register an item.
103    *
104    * Some works(e.g, lighting) of SceneView should be propagated to the child 3D items.
105    * SceneView can avoid unnecessary tree traversal to find 3D items by storing only 3D items as a list.
106    *
107    * @param[in] item scene observer to be registered.
108    */
109   void RegisterSceneItem(Scene3D::Internal::LightObserver* item);
110
111   /**
112    * @brief Unregister an item
113    *
114    * @param[in] item scene observer to be unregistered.
115    */
116   void UnregisterSceneItem(Scene3D::Internal::LightObserver* item);
117
118   /**
119    * @copydoc SceneView::SetImageBasedLightSource()
120    */
121   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
122
123   /**
124    * @copydoc SceneView::SetImageBasedLightScaleFactor()
125    */
126   void SetImageBasedLightScaleFactor(float scaleFactor);
127
128   /**
129    * @copydoc SceneView::GetImageBasedLightScaleFactor()
130    */
131   float GetImageBasedLightScaleFactor() const;
132
133   /**
134    * @brief Adds a Light object to this SceneView.
135    * Multiple light object can be added on this SceneView
136    * But the number of lights those actually turned on has limitation.
137    * Scene3D::Light::GetMaximumEnabledLightCount() method can be used to know the maximum namber.
138    *
139    * @param[in] light Light object to be added.
140    */
141   void AddLight(Scene3D::Light light);
142
143   /**
144    * @brief Removes a Light object to this SceneView.
145    * If the light is currently turned on, and SceneView has lights more than maximum number of enabled light.
146    * One of light in queue is turned on after this light is removed.
147    *
148    * @param[in] light Light object to be removed.
149    */
150   void RemoveLight(Scene3D::Light light);
151
152   /**
153    * @brief Set a shadow to this scene by input light.
154    * Currently, SceneView supports only one shadow.
155    *
156    * @param[in] light Light object to make shadow.
157    * @note The shadow will be drawn if the input light is turn on in current scene.
158    */
159   void SetShadow(Scene3D::Light light);
160
161   /**
162    * @brief Removes Shadow from this SceneView.
163    *
164    * @param[in] light Light object to be removed.
165    */
166   void RemoveShadow(Scene3D::Light light);
167
168   /**
169    * @copydoc SceneView::GetActivatedLightCount()
170    */
171   uint32_t GetActivatedLightCount() const;
172
173   /**
174    * @copydoc SceneView::UseFramebuffer()
175    */
176   void UseFramebuffer(bool useFramebuffer);
177
178   /**
179    * @copydoc SceneView::IsUsingFramebuffer()
180    */
181   bool IsUsingFramebuffer() const;
182
183   /**
184    * @copydoc SceneView::SetResolution()
185    */
186   void SetResolution(uint32_t width, uint32_t height);
187
188   /**
189    * @copydoc SceneView::GetResolutionWidth()
190    */
191   uint32_t GetResolutionWidth();
192
193   /**
194    * @copydoc SceneView::GetResolutionHeight()
195    */
196   uint32_t GetResolutionHeight();
197
198   /**
199    * @copydoc SceneView::ResetResolution()
200    */
201   void ResetResolution();
202
203   /**
204    * @copydoc SceneView::SetFramebufferMultiSamplingLevel()
205    */
206   void SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel);
207
208   /**
209    * @copydoc SceneView::GetFramebufferMultiSamplingLevel()
210    */
211   uint8_t GetFramebufferMultiSamplingLevel() const;
212
213   /**
214    * @copydoc SceneView::SetSkybox()
215    */
216   void SetSkybox(const std::string& skyboxUrl);
217
218   /**
219    * @copydoc SceneView::SetSkyboxEnvironmentMapType()
220    */
221   void SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
222
223   /**
224    * @copydoc SceneView::SetSkyboxIntensity()
225    */
226   void SetSkyboxIntensity(float intensity);
227
228   /**
229    * @copydoc SceneView::GetSkyboxIntensity()
230    */
231   float GetSkyboxIntensity() const;
232
233   /**
234    * @copydoc SceneView::SetSkyboxOrientation()
235    */
236   void SetSkyboxOrientation(const Quaternion& orientation);
237
238   /**
239    * @copydoc SceneView::GetSkyboxOrientation()
240    */
241   Quaternion GetSkyboxOrientation() const;
242
243   /**
244    * @brief Retrieves ShaderManager of this SceneView.
245    * @return ShaderManager of this SceneView.
246    */
247   Dali::Scene3D::Loader::ShaderManagerPtr GetShaderManager() const;
248
249   /**
250    * @brief Updates shader uniforms about shadow.
251    * @param[in] light Light that makes shadow.
252    */
253   void UpdateShadowUniform(Scene3D::Light light);
254
255   /**
256    * @brief Sets alpha mask url
257    * @param[in] alphaMaskUrl Url for alpha mask.
258    */
259   void SetAlphaMaskUrl(std::string& alphaMaskUrl);
260
261   /**
262    * @brief Retrieves alpha mask url
263    * @return Alpha mask url.
264    */
265   std::string GetAlphaMaskUrl();
266
267   /**
268    * @brief Sets mask content scale factor
269    * @param[in] maskContentScaleFactor Scale factor for mask content.
270    */
271   void SetMaskContentScaleFactor(float maskContentScaleFactor);
272
273   /**
274    * @brief Retrieves mask content scale factor
275    * @return Scale factor for mask content.
276    */
277   float GetMaskContentScaleFactor();
278
279   /**
280    * @brief Sets whether the rendered result will be crop to mask or not.
281    * @param[in] enableCropToMask True for crop rendered result to mask.
282    */
283   void EnableCropToMask(bool enableCropToMask);
284
285   /**
286    * @brief Retrieves whether the crop to mask is enabled or not.
287    * @return True when rendered result is cropped to mask.
288    */
289   bool IsEnabledCropToMask();
290
291   /**
292    * @brief Gets current RenderTask
293    */
294   Dali::RenderTask GetRenderTask();
295
296   // Properties
297
298   /**
299    * Called when a property of an object of this type is set.
300    * @param[in] object The object whose property is set.
301    * @param[in] index The property index.
302    * @param[in] value The new property value.
303    */
304   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
305
306   /**
307    * Called to retrieve a property of an object of this type.
308    * @param[in] object The object whose property is to be retrieved.
309    * @param[in] index The property index.
310    * @return The current value of the property.
311    */
312   static Property::Value GetProperty(BaseObject* object, Property::Index index);
313
314 protected:
315   /**
316    * @brief Constructs a new SceneView.
317    */
318   SceneView();
319
320   /**
321    * A reference counted object may only be deleted by calling Unreference()
322    */
323   virtual ~SceneView();
324
325 private:
326   /**
327    * @copydoc CustomActorImpl::OnSceneConnection()
328    */
329   void OnSceneConnection(int depth) override;
330
331   /**
332    * @copydoc CustomActorImpl::OnSceneDisconnection()
333    */
334   void OnSceneDisconnection() override;
335
336   /**
337    * @copydoc Toolkit::Control::OnInitialize()
338    */
339   void OnInitialize() override;
340
341   /**
342    * @copydoc Toolkit::Control::OnChildAdd()
343    */
344   void OnChildAdd(Actor& child) override;
345
346   /**
347    * @copydoc Toolkit::Control::OnChildRemove()
348    */
349   void OnChildRemove(Actor& child) override;
350
351   /**
352    * @copydoc Toolkit::Control::GetHeightForWidth()
353    */
354   float GetHeightForWidth(float width) override;
355
356   /**
357    * @copydoc Toolkit::Control::GetWidthForHeight()
358    */
359   float GetWidthForHeight(float height) override;
360
361   /**
362    * @copydoc Toolkit::Control::OnRelayout()
363    */
364   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
365
366   /**
367    * @copydoc Toolkit::Control::IsResourceReady()
368    */
369   bool IsResourceReady() const override;
370
371   /**
372    * @brief Changes main camera as a input camera
373    *
374    * @param camera CameraActor that will be a main camera of the SceneView
375    */
376   void UpdateCamera(CameraActor camera);
377
378   /**
379    * @brief Updates RenderTask to use selected camera and to make framebuffer
380    */
381   void UpdateRenderTask();
382
383   /**
384    * @brief Callback that will be called when window is resized.
385    */
386   void OnWindowResized(Window window, Window::WindowSize size);
387
388   /**
389    * @brief Updates camera's projection orientation according to the screen orientation.
390    */
391   void RotateCamera();
392
393   /**
394    * @brief UpdateSkybox with skybox url and skybox environment map type.
395    *
396    * @param[in] skyboxUrl image url for skybox.
397    * @param[in] skyboxEnvironmentMapType The environment type of skybox.
398    */
399   void UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
400
401   /**
402    * @brief Asynchronously skybox loading finished.
403    */
404   void OnSkyboxLoadComplete();
405
406   /**
407    * @brief Asynchronously ibl diffusel image loading finished.
408    */
409   void OnIblDiffuseLoadComplete();
410
411   /**
412    * @brief Asynchronously ibl specular image loading finished.
413    */
414   void OnIblSpecularLoadComplete();
415
416   /**
417    * @brief Asynchronously ibl loading finished.
418    */
419   void OnIblLoadComplete();
420
421   /**
422    * @brief Notify the changes of Ibl textures to the child items.
423    */
424   void NotifyImageBasedLightTextureChange();
425
426   /**
427    * @brief Update shadowMap framebuffer when the size should be changed.
428    * @param[in] shadowMapSize The size of shadowMap texture. The texture's width and hight is equal.
429    */
430   void UpdateShadowMapBuffer(uint32_t shadowMapSize);
431
432 private:
433   Toolkit::Visual::Base mVisual;
434
435   /////////////////////////////////////////////////////////////
436   // FrameBuffer and Rendertask to render child objects as a 3D Scene
437   Dali::WeakHandle<Dali::Window>                 mWindow;
438   Integration::SceneHolder                       mSceneHolder;
439   CameraActor                                    mDefaultCamera;
440   CameraActor                                    mSelectedCamera;
441   std::vector<CameraActor>                       mCameras;
442   std::vector<Scene3D::Internal::LightObserver*> mItems;
443   Dali::FrameBuffer                              mFrameBuffer;
444   Dali::Texture                                  mTexture;
445   Dali::RenderTask                               mRenderTask;
446   Layer                                          mRootLayer;
447   int32_t                                        mWindowOrientation;
448   Dali::Actor                                    mSkybox;
449   Quaternion                                     mSkyboxOrientation;
450   float                                          mSkyboxIntensity{1.0f};
451   uint8_t                                        mFrameBufferMultiSamplingLevel{0u};
452
453   bool     mWindowSizeChanged{false};
454   uint32_t mWindowWidth{0};
455   uint32_t mWindowHeight{0};
456
457   // Masking
458   std::string mAlphaMaskUrl;
459   float       mMaskContentScaleFactor{1.0f};
460   bool        mCropToMask{true};
461   bool        mMaskingPropertyChanged{false};
462
463   // Shader Factory
464   Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
465
466   // Light
467   std::vector<std::pair<Scene3D::Light, bool>> mLights; // Pair of Light object and flag that denotes the light is currently activated or not.
468   Dali::FrameBuffer                            mShadowFrameBuffer;
469   Dali::RenderTask                             mShadowMapRenderTask;
470   Scene3D::Light                               mShadowLight;
471   Dali::Texture                                mShadowTexture;
472
473   // Asynchronous Loading.
474   EnvironmentMapLoadTaskPtr mSkyboxLoadTask;
475   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
476   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
477   std::string               mSkyboxUrl;
478   std::string               mDiffuseIblUrl;
479   std::string               mSpecularIblUrl;
480
481   Scene3D::EnvironmentMapType mSkyboxEnvironmentMapType{Scene3D::EnvironmentMapType::AUTO};
482   Dali::Texture               mSkyboxTexture;
483   Dali::Texture               mDiffuseTexture;
484   Dali::Texture               mSpecularTexture;
485   float                       mIblScaleFactor{1.0f};
486   uint32_t                    mSpecularMipmapLevels{1u};
487   bool                        mUseFrameBuffer{false};
488   bool                        mSkyboxResourceReady{true};
489   bool                        mIblDiffuseResourceReady{true};
490   bool                        mIblSpecularResourceReady{true};
491   bool                        mSkyboxDirty{false};
492   bool                        mIblDiffuseDirty{false};
493   bool                        mIblSpecularDirty{false};
494 };
495
496 } // namespace Internal
497
498 // Helpers for public-api forwarding methods
499 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
500 {
501   DALI_ASSERT_ALWAYS(obj);
502   Dali::RefObject& handle = obj.GetImplementation();
503   return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
504 }
505
506 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
507 {
508   DALI_ASSERT_ALWAYS(obj);
509   const Dali::RefObject& handle = obj.GetImplementation();
510   return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
511 }
512
513 } // namespace Scene3D
514
515 } // namespace Dali
516
517 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H