Asynchronous loading of Scene3D resources
[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) 2022 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/image-loader/async-image-loader.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/public-api/controls/scene-view/scene-view.h>
36 #include <dali-scene3d/internal/common/environment-map-load-task.h>
37 #include <dali-scene3d/internal/common/image-based-light-observer.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::ImageBasedLightObserver* item);
109
110   /**
111    * @brief Unregister an item
112    *
113    * @param[in] item scene observer to be unregistered.
114    */
115   void UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* 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    * @copydoc SceneView::UseFramebuffer()
134    */
135   void UseFramebuffer(bool useFramebuffer);
136
137   /**
138    * @copydoc SceneView::IsUsingFramebuffer()
139    */
140   bool IsUsingFramebuffer() const;
141
142   /**
143    * @copydoc SceneView::SetSkybox()
144    */
145   void SetSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType);
146
147   /**
148    * @copydoc SceneView::SetSkyboxIntensity()
149    */
150   void SetSkyboxIntensity(float intensity);
151
152   /**
153    * @copydoc SceneView::GetSkyboxIntensity()
154    */
155   float GetSkyboxIntensity() const;
156
157   /**
158    * @copydoc SceneView::SetSkyboxOrientation()
159    */
160   void SetSkyboxOrientation(const Quaternion& orientation);
161
162   /**
163    * @copydoc SceneView::GetSkyboxOrientation()
164    */
165   Quaternion GetSkyboxOrientation() const;
166
167 protected:
168   /**
169    * @brief Constructs a new SceneView.
170    */
171   SceneView();
172
173   /**
174    * A reference counted object may only be deleted by calling Unreference()
175    */
176   virtual ~SceneView();
177
178 private:
179   /**
180    * @copydoc CustomActorImpl::OnSceneConnection()
181    */
182   void OnSceneConnection(int depth) override;
183
184   /**
185    * @copydoc CustomActorImpl::OnSceneDisconnection()
186    */
187   void OnSceneDisconnection() override;
188
189   /**
190    * @copydoc Toolkit::Control::OnInitialize()
191    */
192   void OnInitialize() override;
193
194   /**
195    * @copydoc Toolkit::Control::OnChildAdd()
196    */
197   void OnChildAdd(Actor& child) override;
198
199   /**
200    * @copydoc Toolkit::Control::OnChildRemove()
201    */
202   void OnChildRemove(Actor& child) override;
203
204   /**
205    * @copydoc Toolkit::Control::GetHeightForWidth()
206    */
207   float GetHeightForWidth(float width) override;
208
209   /**
210    * @copydoc Toolkit::Control::GetWidthForHeight()
211    */
212   float GetWidthForHeight(float height) override;
213
214   /**
215    * @copydoc Toolkit::Control::OnRelayout()
216    */
217   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
218
219   /**
220    * @copydoc Toolkit::Control::IsResourceReady()
221    */
222   bool IsResourceReady() const override;
223
224   /**
225    * @brief Changes main camera as a input camera
226    *
227    * @param camera CameraActor that will be a main camera of the SceneView
228    */
229   void UpdateCamera(CameraActor camera);
230
231   /**
232    * @brief Updates RenderTask to use selected camera and to make framebuffer
233    */
234   void UpdateRenderTask();
235
236   /**
237    * @brief Callback that will be called when window is resized.
238    */
239   void OnWindowResized(Window window, Window::WindowSize size);
240
241   /**
242    * @brief Updates camera's projection orientation according to the screen orientation.
243    */
244   void RotateCamera();
245
246   /**
247    * @brief Asynchronously skybox (Equirectangular) loading finished.
248    */
249   void OnSkyboxEquirectangularLoadComplete(uint32_t loadedTaskId, PixelData pixelData);
250
251   /**
252    * @brief Asynchronously skybox loading finished.
253    */
254   void OnSkyboxLoadComplete();
255
256   /**
257    * @brief Asynchronously ibl diffusel image loading finished.
258    */
259   void OnIblDiffuseLoadComplete();
260
261   /**
262    * @brief Asynchronously ibl specular image loading finished.
263    */
264   void OnIblSpecularLoadComplete();
265
266   /**
267    * @brief Asynchronously ibl loading finished.
268    */
269   void OnIblLoadComplete();
270
271   /**
272    * @brief Notify the changes of Ibl textures to the child items.
273    */
274   void NotifyImageBasedLightTextureChange();
275
276 private:
277   Toolkit::Visual::Base mVisual;
278
279   /////////////////////////////////////////////////////////////
280   // FrameBuffer and Rendertask to render child objects as a 3D Scene
281   Dali::WeakHandle<Dali::Window>                           mWindow;
282   CameraActor                                              mDefaultCamera;
283   CameraActor                                              mSelectedCamera;
284   std::vector<CameraActor>                                 mCameras;
285   std::vector<Scene3D::Internal::ImageBasedLightObserver*> mItems;
286   Dali::FrameBuffer                                        mFrameBuffer;
287   Dali::Texture                                            mTexture;
288   Dali::RenderTask                                         mRenderTask;
289   Layer                                                    mRootLayer;
290   int32_t                                                  mWindowOrientation;
291   Dali::Actor                                              mSkybox;
292   Quaternion                                               mSkyboxOrientation;
293   float                                                    mSkyboxIntensity{1.0f};
294
295   // Asynchronous Loading.
296   EnvironmentMapLoadTaskPtr       mSkyboxLoadTask;
297   EnvironmentMapLoadTaskPtr       mIblDiffuseLoadTask;
298   EnvironmentMapLoadTaskPtr       mIblSpecularLoadTask;
299   std::string                     mSkyboxUrl;
300   std::string                     mDiffuseIblUrl;
301   std::string                     mSpecularIblUrl;
302   Dali::Toolkit::AsyncImageLoader mSkyboxImageLoader;
303   uint32_t                        mSkyboxImageId{0u};
304
305   Scene3D::SceneView::SkyboxType mSkyboxEnvironmentMapType;
306   Dali::Texture                  mSkyboxTexture;
307   Dali::Texture                  mDiffuseTexture;
308   Dali::Texture                  mSpecularTexture;
309   float                          mIblScaleFactor{1.0f};
310   bool                           mUseFrameBuffer{false};
311   bool                           mSkyboxResourceReady{true};
312   bool                           mIblDiffuseResourceReady{true};
313   bool                           mIblSpecularResourceReady{true};
314   bool                           mSkyboxDirty{false};
315   bool                           mIblDiffuseDirty{false};
316   bool                           mIblSpecularDirty{false};
317
318   // TODO : Light Source
319 };
320
321 } // namespace Internal
322
323 // Helpers for public-api forwarding methods
324 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
325 {
326   DALI_ASSERT_ALWAYS(obj);
327   Dali::RefObject& handle = obj.GetImplementation();
328   return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
329 }
330
331 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
332 {
333   DALI_ASSERT_ALWAYS(obj);
334   const Dali::RefObject& handle = obj.GetImplementation();
335   return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
336 }
337
338 } // namespace Scene3D
339
340 } // namespace Dali
341
342 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H