Cache manager for 3D models
[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 // INTERNAL INCLUDES
26 #include <dali-scene3d/internal/common/model-cache-manager.h>
27 #include <dali-scene3d/public-api/loader/animation-definition.h>
28 #include <dali-scene3d/public-api/loader/camera-parameters.h>
29 #include <dali-scene3d/public-api/loader/dli-loader.h>
30 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
31 #include <dali-scene3d/public-api/loader/light-parameters.h>
32 #include <dali-scene3d/public-api/loader/node-definition.h>
33 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
34
35 namespace Dali
36 {
37 namespace Scene3D
38 {
39 namespace Internal
40 {
41 namespace
42 {
43 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
44
45 static constexpr std::string_view OBJ_EXTENSION      = ".obj";
46 static constexpr std::string_view GLTF_EXTENSION     = ".gltf";
47 static constexpr std::string_view DLI_EXTENSION      = ".dli";
48 static constexpr std::string_view METADATA_EXTENSION = "metadata";
49 } // namespace
50
51 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
52 : AsyncTask(callback),
53   mModelUrl(modelUrl),
54   mResourceDirectoryUrl(resourceDirectoryUrl),
55   mHasSucceeded(false),
56   mModelCacheManager(ModelCacheManager::Get()),
57   mLoadResult(mModelCacheManager.GetModelLoadResult(modelUrl))
58 {
59 }
60
61 ModelLoadTask::~ModelLoadTask()
62 {
63 }
64
65 void ModelLoadTask::Process()
66 {
67   uint32_t               cacheRefCount                  = mModelCacheManager.GetModelCacheRefCount(mModelUrl);
68   Dali::ConditionalWait& loadSceneConditionalWait       = mModelCacheManager.GetLoadSceneConditionalWaitInstance(mModelUrl);
69   Dali::ConditionalWait& loadRawResourceConditionalWait = mModelCacheManager.GetLoadRawResourceConditionalWaitInstance(mModelUrl);
70
71   std::filesystem::path modelUrl(mModelUrl);
72   if(mResourceDirectoryUrl.empty())
73   {
74     mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
75   }
76   std::string extension = modelUrl.extension();
77   std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
78
79   Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type) {
80     return mResourceDirectoryUrl;
81   };
82
83   {
84     ConditionalWait::ScopedLock lock(loadSceneConditionalWait);
85
86     while(cacheRefCount > 1 && mModelCacheManager.IsSceneLoading(mModelUrl))
87     {
88       loadSceneConditionalWait.Wait();
89     }
90   }
91
92   {
93     ConditionalWait::ScopedLock lock(loadSceneConditionalWait);
94
95     if(!mModelCacheManager.IsSceneLoaded(mModelUrl))
96     {
97       mModelCacheManager.SetSceneLoading(mModelUrl, true);
98
99       std::filesystem::path metaDataUrl = modelUrl;
100       metaDataUrl.replace_extension(METADATA_EXTENSION.data());
101
102       Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
103
104       mLoadResult.mAnimationDefinitions.clear();
105
106       if(extension == DLI_EXTENSION)
107       {
108         Dali::Scene3D::Loader::DliLoader              loader;
109         Dali::Scene3D::Loader::DliLoader::InputParams input{
110           pathProvider(Dali::Scene3D::Loader::ResourceType::Mesh),
111           nullptr,
112           {},
113           {},
114           nullptr,
115           {}};
116         Dali::Scene3D::Loader::DliLoader::LoadParams loadParams{input, mLoadResult};
117         if(!loader.LoadScene(mModelUrl, loadParams))
118         {
119           DALI_LOG_ERROR("Failed to load scene from '%s': %s\n", mModelUrl.c_str(), loader.GetParseError().c_str());
120
121           mModelCacheManager.SetSceneLoaded(mModelUrl, false);
122           mModelCacheManager.SetSceneLoading(mModelUrl, false);
123           mModelCacheManager.UnreferenceModelCache(mModelUrl);
124
125           return;
126         }
127       }
128       else if(extension == GLTF_EXTENSION)
129       {
130         Dali::Scene3D::Loader::ShaderDefinitionFactory sdf;
131         sdf.SetResources(mLoadResult.mResources);
132         Dali::Scene3D::Loader::LoadGltfScene(mModelUrl, sdf, mLoadResult);
133       }
134       else
135       {
136         DALI_LOG_ERROR("Unsupported model type.\n");
137
138         mModelCacheManager.SetSceneLoaded(mModelUrl, false);
139         mModelCacheManager.SetSceneLoading(mModelUrl, false);
140         mModelCacheManager.UnreferenceModelCache(mModelUrl);
141
142         return;
143       }
144
145       mModelCacheManager.SetSceneLoaded(mModelUrl, true);
146       mModelCacheManager.SetSceneLoading(mModelUrl, false);
147     }
148   }
149
150   loadSceneConditionalWait.Notify();
151
152   {
153     ConditionalWait::ScopedLock lock(loadRawResourceConditionalWait);
154
155     while(cacheRefCount > 1 && mLoadResult.mResources.mRawResourcesLoading)
156     {
157       loadRawResourceConditionalWait.Wait();
158     }
159   }
160
161   {
162     ConditionalWait::ScopedLock lock(loadRawResourceConditionalWait);
163
164     for(auto iRoot : mLoadResult.mScene.GetRoots())
165     {
166       mResourceRefCounts.push_back(mLoadResult.mResources.CreateRefCounter());
167       mLoadResult.mScene.CountResourceRefs(iRoot, mResourceChoices, mResourceRefCounts.back());
168       mLoadResult.mResources.CountEnvironmentReferences(mResourceRefCounts.back());
169
170       mLoadResult.mResources.LoadRawResources(mResourceRefCounts.back(), pathProvider);
171
172       // glTF Mesh is defined in right hand coordinate system, with positive Y for Up direction.
173       // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
174       for(auto&& env : mLoadResult.mResources.mEnvironmentMaps)
175       {
176         env.first.mYDirection = Y_DIRECTION;
177       }
178     }
179   }
180
181   loadRawResourceConditionalWait.Notify();
182
183   mHasSucceeded = true;
184 }
185
186 bool ModelLoadTask::IsReady()
187 {
188   return true;
189 }
190
191 bool ModelLoadTask::HasSucceeded() const
192 {
193   return mHasSucceeded;
194 }
195
196 } // namespace Internal
197
198 } // namespace Scene3D
199
200 } // namespace Dali