Merge changes I048d8903,Ib84dd28c into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / common / model-load-task.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/common/model-load-task.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <filesystem>
24
25 namespace Dali
26 {
27 namespace Scene3D
28 {
29 namespace Internal
30 {
31 namespace
32 {
33 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
34 } // namespace
35
36 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
37 : AsyncTask(callback),
38   mModelUrl(modelUrl),
39   mResourceDirectoryUrl(resourceDirectoryUrl),
40   mModelCacheManager(Scene3D::Internal::ModelCacheManager::Get()),
41   mLoadResult(mModelCacheManager.GetModelLoadResult(mModelUrl)),
42   mHasSucceeded(false)
43 {
44 }
45
46 ModelLoadTask::~ModelLoadTask()
47 {
48 }
49
50 void ModelLoadTask::Process()
51 {
52   if(mResourceDirectoryUrl.empty())
53   {
54     std::filesystem::path modelUrl(mModelUrl);
55     mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
56   }
57
58   Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type)
59   {
60     return mResourceDirectoryUrl;
61   };
62
63   mModelLoader = std::make_shared<Dali::Scene3D::Loader::ModelLoader>(mModelUrl, mResourceDirectoryUrl, mLoadResult);
64
65   bool                   loadSucceeded            = false;
66   Dali::ConditionalWait& loadSceneConditionalWait = mModelCacheManager.GetLoadSceneConditionalWaitInstance(mModelUrl);
67   {
68     ConditionalWait::ScopedLock lock(loadSceneConditionalWait);
69     if(mModelCacheManager.IsSceneLoaded(mModelUrl))
70     {
71       loadSucceeded = true;
72     }
73     else
74     {
75       mModelCacheManager.SetSceneLoading(mModelUrl, true);
76
77       loadSucceeded = mModelLoader->LoadModel(pathProvider, true);
78
79       // Mesh of glTF and dli is defined in right hand coordinate system, with positive Y for Up direction.
80       // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
81       for(auto&& env : GetResources().mEnvironmentMaps)
82       {
83         env.first.mYDirection = Y_DIRECTION;
84       }
85
86       mModelCacheManager.SetSceneLoading(mModelUrl, false);
87       mModelCacheManager.SetSceneLoaded(mModelUrl, loadSucceeded);
88     }
89   }
90
91   if(!loadSucceeded)
92   {
93     DALI_LOG_ERROR("Failed to load scene from '%s'\n", mModelUrl.c_str());
94     return;
95   }
96
97   mHasSucceeded = true;
98 }
99
100 bool ModelLoadTask::IsReady()
101 {
102   return true;
103 }
104
105 bool ModelLoadTask::HasSucceeded() const
106 {
107   return mHasSucceeded;
108 }
109
110 Dali::Scene3D::Loader::SceneDefinition& ModelLoadTask::GetScene() const
111 {
112   return mModelLoader->GetScene();
113 }
114
115 Dali::Scene3D::Loader::ResourceBundle& ModelLoadTask::GetResources() const
116 {
117   return mModelLoader->GetResources();
118 }
119
120 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoadTask::GetAnimations() const
121 {
122   return mModelLoader->GetAnimations();
123 }
124
125 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoadTask::GetCameras() const
126 {
127   return mModelLoader->GetCameras();
128 }
129
130 Dali::Scene3D::Loader::Customization::Choices& ModelLoadTask::GetResourceChoices()
131 {
132   return mModelLoader->GetResourceChoices();
133 }
134
135 } // namespace Internal
136
137 } // namespace Scene3D
138
139 } // namespace Dali