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