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