Cache manager for 3D models
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / model / model-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_MODEL_H
2 #define DALI_SCENE3D_INTERNAL_MODEL_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/public-api/controls/control-impl.h>
23 #include <dali/public-api/actors/camera-actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/animation/animation.h>
26 #include <dali/public-api/object/weak-handle.h>
27 #include <dali/public-api/rendering/texture.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-scene3d/internal/common/environment-map-load-task.h>
31 #include <dali-scene3d/internal/common/image-based-light-observer.h>
32 #include <dali-scene3d/internal/common/model-load-task.h>
33 #include <dali-scene3d/public-api/controls/model/model.h>
34 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
35 #include <dali-scene3d/public-api/loader/load-result.h>
36
37 namespace Dali
38 {
39 namespace Scene3D
40 {
41 class Model;
42
43 namespace Internal
44 {
45 /**
46  * @brief Impl class for Model.
47  */
48 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
49 {
50 public:
51   using AnimationData = std::pair<std::string, Dali::Animation>;
52   using CameraData    = Loader::CameraParameters;
53
54   /**
55    * @copydoc Model::New()
56    */
57   static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
58
59   /**
60    * @copydoc Model::GetModelRoot()
61    */
62   const Actor GetModelRoot() const;
63
64   /**
65    * @copydoc Model::SetChildrenSensitive()
66    */
67   void SetChildrenSensitive(bool enable);
68
69   /**
70    * @copydoc Model::GetChildrenSensitive()
71    */
72   bool GetChildrenSensitive() const;
73
74   /**
75    * @copydoc Model::SetChildrenFocusable()
76    */
77   void SetChildrenFocusable(bool enable);
78
79   /**
80    * @copydoc Model::GetChildrenFocusable()
81    */
82   bool GetChildrenFocusable() const;
83
84   /**
85    * @copydoc Model::SetImageBasedLightSource()
86    */
87   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
88
89   /**
90    * @copydoc Model::SetImageBasedLightTexture()
91    */
92   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
93
94   /**
95    * @copydoc Model::SetImageBasedLightScaleFactor()
96    */
97   void SetImageBasedLightScaleFactor(float scaleFactor);
98
99   /**
100    * @copydoc Model::GetImageBasedLightScaleFactor()
101    */
102   float GetImageBasedLightScaleFactor() const;
103
104   /**
105    * @copydoc Model::GetAnimationCount()
106    */
107   uint32_t GetAnimationCount() const;
108
109   /**
110    * @copydoc Model::GetAnimation()
111    */
112   Dali::Animation GetAnimation(uint32_t index) const;
113
114   /**
115    * @copydoc Model::GetAnimation()
116    */
117   Dali::Animation GetAnimation(const std::string& name) const;
118
119   /**
120    * @copydoc Model::GetCameraCount()
121    */
122   uint32_t GetCameraCount() const;
123
124   /**
125    * @copydoc Model::GenerateCamera()
126    */
127   Dali::CameraActor GenerateCamera(uint32_t index) const;
128
129   /**
130    * @copydoc Model::ApplyCamera()
131    */
132   bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
133
134 protected:
135   /**
136    * @brief Constructs a new Model.
137    * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
138    * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
139    */
140   Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
141
142   /**
143    * A reference counted object may only be deleted by calling Unreference()
144    */
145   virtual ~Model();
146
147 private:
148   /**
149    * @copydoc Toolkit::Control::OnInitialize
150    */
151   void OnInitialize();
152
153   /**
154    * @copydoc CustomActorImpl::OnSceneConnection()
155    */
156   void OnSceneConnection(int depth) override;
157
158   /**
159    * @copydoc CustomActorImpl::OnSceneDisconnection()
160    */
161   void OnSceneDisconnection() override;
162
163   /**
164    * @copydoc Toolkit::Control::GetNaturalSize
165    */
166   Vector3 GetNaturalSize() override;
167
168   /**
169    * @copydoc Toolkit::Control::GetHeightForWidth()
170    */
171   float GetHeightForWidth(float width) override;
172
173   /**
174    * @copydoc Toolkit::Control::GetWidthForHeight()
175    */
176   float GetWidthForHeight(float height) override;
177
178   /**
179    * @copydoc Toolkit::Control::OnRelayout()
180    */
181   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
182
183   /**
184    * @copydoc Toolkit::Control::IsResourceReady()
185    */
186   bool IsResourceReady() const override;
187
188 private:
189   /**
190    * @brief Scales the model to fit the control or to return to original size.
191    */
192   void ScaleModel();
193
194   /**
195    * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
196    */
197   void FitModelPosition();
198
199   /**
200    * @brief Changes IBL information of the input node.
201    */
202   void CollectRenderableActor(Actor actor);
203
204   /**
205    * @brief Changes IBL textures of the input node.
206    */
207   void UpdateImageBasedLightTexture();
208
209   /**
210    * @brief Changes IBL scale factor of the input node.
211    */
212   void UpdateImageBasedLightScaleFactor();
213
214   /**
215    * @brief Apply self transform into inputed camera.
216    * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
217    * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
218    *
219    * @param[in,out] camera CameraActor who need to apply model itself's transform
220    */
221   void ApplyCameraTransform(Dali::CameraActor camera) const;
222
223 public: // Overrides ImageBasedLightObserver Methods.
224   /**
225    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
226    */
227   void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
228
229   /**
230    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
231    */
232   void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
233
234 private:
235   /**
236    * @brief Asynchronously model loading finished.
237    */
238   void OnModelLoadComplete();
239
240   /**
241    * @brief Asynchronously ibl diffusel image loading finished.
242    */
243   void OnIblDiffuseLoadComplete();
244
245   /**
246    * @brief Asynchronously ibl specular image loading finished.
247    */
248   void OnIblSpecularLoadComplete();
249
250   /**
251    * @brief Asynchronously ibl loading finished.
252    */
253   void OnIblLoadComplete();
254
255   /**
256    * @brief Reset Resource loading tasks.
257    */
258   void ResetResourceTasks();
259
260   /**
261    * @brief Reset a Resource loading task.
262    */
263   void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
264
265   /**
266    * @brief Request to load a Ibl texture asynchronously
267    */
268   void RequestLoadIblTexture(EnvironmentMapLoadTaskPtr asyncLoadTask, const std::string& url);
269
270   /**
271    * @brief Notify Resource Ready signal.
272    */
273   void NotifyResourceReady();
274
275   /**
276    * @brief Create Model from loaded SceneDefinition.
277    */
278   void CreateModel();
279
280   /**
281    * @brief Create Dali::Animation from loaded AnimationDefinitions.
282    */
283   void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
284
285   /**
286    * @brief Reset CameraData from loaded CameraParameters.
287    */
288   void ResetCameraParameters();
289
290 private:
291   std::string                    mModelUrl;
292   std::string                    mResourceDirectoryUrl;
293   Dali::Actor                    mModelRoot;
294   std::vector<AnimationData>     mAnimations;
295   std::vector<CameraData>        mCameraParameters;
296   std::vector<WeakHandle<Actor>> mRenderableActors;
297   WeakHandle<Scene3D::SceneView> mParentSceneView;
298
299   // Asynchronous loading variable
300   ModelLoadTaskPtr          mModelLoadTask;
301   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
302   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
303
304   std::string mDiffuseIblUrl;
305   std::string mSpecularIblUrl;
306
307   // TODO: This default texture can be removed after 3D Resource Cache is added.
308   Dali::Texture mDefaultSpecularTexture;
309   Dali::Texture mDefaultDiffuseTexture;
310   Dali::Texture mSceneSpecularTexture;
311   Dali::Texture mSceneDiffuseTexture;
312   Dali::Texture mSpecularTexture;
313   Dali::Texture mDiffuseTexture;
314   Vector3       mNaturalSize;
315   Vector3       mModelPivot;
316   float         mSceneIblScaleFactor;
317   float         mIblScaleFactor;
318   bool          mModelChildrenSensitive;
319   bool          mModelChildrenFocusable;
320   bool          mModelResourceReady;
321   bool          mIblDiffuseResourceReady;
322   bool          mIblSpecularResourceReady;
323   bool          mIblDiffuseDirty;
324   bool          mIblSpecularDirty;
325 };
326
327 } // namespace Internal
328
329 // Helpers for public-api forwarding methods
330 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
331 {
332   DALI_ASSERT_ALWAYS(obj);
333   Dali::RefObject& handle = obj.GetImplementation();
334   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
335 }
336
337 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
338 {
339   DALI_ASSERT_ALWAYS(obj);
340   const Dali::RefObject& handle = obj.GetImplementation();
341   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
342 }
343
344 } // namespace Scene3D
345
346 } // namespace Dali
347
348 #endif // DALI_SCENE3D_INTERNAL_MODEL_H