[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / common / model-load-task.cpp
1 /*
2  * Copyright (c) 2024 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, AsyncTask::PriorityType::LOW),
38   mModelUrl(modelUrl),
39   mResourceDirectoryUrl(resourceDirectoryUrl),
40   mModelCacheManager(Scene3D::Internal::ModelCacheManager::Get()),
41   mLoadResult(mModelCacheManager.GetModelLoadResult(mModelUrl)),
42   mHasSucceeded(false)
43 {
44   mModelCacheManager.ReferenceModelCache(mModelUrl);
45 }
46
47 ModelLoadTask::~ModelLoadTask()
48 {
49   mModelCacheManager.UnreferenceModelCache(mModelUrl);
50 }
51
52 void ModelLoadTask::Process()
53 {
54   if(mResourceDirectoryUrl.empty())
55   {
56     std::filesystem::path modelUrl(mModelUrl);
57     mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
58   }
59
60   Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type) {
61     return mResourceDirectoryUrl;
62   };
63
64   mModelLoader = std::make_shared<Dali::Scene3D::Loader::ModelLoader>(mModelUrl, mResourceDirectoryUrl, mLoadResult);
65
66   bool loadSucceeded = false;
67   {
68     // Lock model url during process, so let we do not try to load same model multiple times.
69     mModelCacheManager.LockModelLoadScene(mModelUrl);
70     if(mModelCacheManager.IsSceneLoaded(mModelUrl))
71     {
72       loadSucceeded = true;
73     }
74     else
75     {
76       mModelCacheManager.SetSceneLoading(mModelUrl, true);
77
78       loadSucceeded = mModelLoader->LoadModel(pathProvider, true);
79
80       // Mesh of glTF and dli is defined in right hand coordinate system, with positive Y for Up direction.
81       // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
82       for(auto&& env : GetResources().mEnvironmentMaps)
83       {
84         env.first.mYDirection = Y_DIRECTION;
85       }
86
87       mModelCacheManager.SetSceneLoading(mModelUrl, false);
88       mModelCacheManager.SetSceneLoaded(mModelUrl, loadSucceeded);
89     }
90     mModelCacheManager.UnlockModelLoadScene(mModelUrl);
91   }
92
93   if(!loadSucceeded)
94   {
95     DALI_LOG_ERROR("Failed to load scene from '%s'\n", mModelUrl.c_str());
96     return;
97   }
98
99   mHasSucceeded = true;
100 }
101
102 bool ModelLoadTask::IsReady()
103 {
104   return true;
105 }
106
107 bool ModelLoadTask::HasSucceeded() const
108 {
109   return mHasSucceeded;
110 }
111
112 Dali::Scene3D::Loader::SceneDefinition& ModelLoadTask::GetScene() const
113 {
114   return mModelLoader->GetScene();
115 }
116
117 Dali::Scene3D::Loader::ResourceBundle& ModelLoadTask::GetResources() const
118 {
119   return mModelLoader->GetResources();
120 }
121
122 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoadTask::GetAnimations() const
123 {
124   return mModelLoader->GetAnimations();
125 }
126
127 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoadTask::GetCameras() const
128 {
129   return mModelLoader->GetCameras();
130 }
131
132 Dali::Scene3D::Loader::Customization::Choices& ModelLoadTask::GetResourceChoices()
133 {
134   return mModelLoader->GetResourceChoices();
135 }
136
137 } // namespace Internal
138
139 } // namespace Scene3D
140
141 } // namespace Dali