Merge "Add log for font load validation" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / resource-bundle.cpp
1 /*
2  * Copyright (c) 2021 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 // FILE HEADER
19 #include "dali-scene-loader/public-api/resource-bundle.h"
20
21 // EXTERNAL
22 #include <cstring>
23 #include <fstream>
24 #include <istream>
25 #include "dali-toolkit/public-api/image-loader/sync-image-loader.h"
26 #include "dali/public-api/rendering/sampler.h"
27
28 namespace Dali
29 {
30 using namespace Toolkit;
31
32 namespace SceneLoader
33 {
34 namespace
35 {
36 const char* const RESOURCE_TYPE_NAMES[] = {
37   "Environment",
38   "Shader",
39   "Mesh",
40   "Material",
41 };
42
43 } // namespace
44
45 const char* GetResourceTypeName(ResourceType::Value type)
46 {
47   return RESOURCE_TYPE_NAMES[static_cast<int>(type)];
48 }
49
50 ResourceRefCounts ResourceBundle::CreateRefCounter() const
51 {
52   ResourceRefCounts refCounts(4);
53   refCounts[ResourceType::Environment].Resize(mEnvironmentMaps.size(), 0u);
54   refCounts[ResourceType::Shader].Resize(mShaders.size(), 0u);
55   refCounts[ResourceType::Mesh].Resize(mMeshes.size(), 0u);
56   refCounts[ResourceType::Material].Resize(mMaterials.size(), 0u);
57   return refCounts;
58 }
59
60 void ResourceBundle::CountEnvironmentReferences(ResourceRefCounts& refCounts) const
61 {
62   auto& environmentRefCounts = refCounts[ResourceType::Environment];
63
64   const auto& materialRefs = refCounts[ResourceType::Material];
65   for(uint32_t i = 0, iEnd = materialRefs.Size(); i != iEnd; ++i)
66   {
67     if(materialRefs[i] > 0)
68     {
69       ++environmentRefCounts[mMaterials[i].first.mEnvironmentIdx];
70     }
71   }
72 }
73
74 void ResourceBundle::LoadResources(const ResourceRefCounts& refCounts, PathProvider pathProvider, Options::Type options)
75 {
76   const auto kForceLoad  = MaskMatch(options, Options::ForceReload);
77   const auto kKeepUnused = MaskMatch(options, Options::KeepUnused);
78
79   const auto& refCountEnvMaps  = refCounts[ResourceType::Environment];
80   auto        environmentsPath = pathProvider(ResourceType::Environment);
81   for(uint32_t i = 0, iEnd = refCountEnvMaps.Size(); i != iEnd; ++i)
82   {
83     auto  refCount = refCountEnvMaps[i];
84     auto& iEnvMap  = mEnvironmentMaps[i];
85     if(refCount > 0 && (kForceLoad || !iEnvMap.second.IsLoaded()))
86     {
87       auto raw       = iEnvMap.first.LoadRaw(environmentsPath);
88       iEnvMap.second = iEnvMap.first.Load(std::move(raw));
89     }
90     else if(!kKeepUnused && refCount == 0 && iEnvMap.second.IsLoaded())
91     {
92       iEnvMap.second.mDiffuse  = Texture();
93       iEnvMap.second.mSpecular = Texture();
94     }
95   }
96
97   const auto& refCountShaders = refCounts[ResourceType::Shader];
98   auto        shadersPath     = pathProvider(ResourceType::Shader);
99   for(uint32_t i = 0, iEnd = refCountShaders.Size(); i != iEnd; ++i)
100   {
101     auto  refCount = refCountShaders[i];
102     auto& iShader  = mShaders[i];
103     if(refCount > 0 && (kForceLoad || !iShader.second))
104     {
105       auto raw       = iShader.first.LoadRaw(shadersPath);
106       iShader.second = iShader.first.Load(std::move(raw));
107     }
108     else if(!kKeepUnused && refCount == 0 && iShader.second)
109     {
110       iShader.second = Shader();
111     }
112   }
113
114   const auto& refCountMeshes = refCounts[ResourceType::Mesh];
115   auto        modelsPath     = pathProvider(ResourceType::Mesh);
116   for(uint32_t i = 0, iEnd = refCountMeshes.Size(); i != iEnd; ++i)
117   {
118     auto  refCount = refCountMeshes[i];
119     auto& iMesh    = mMeshes[i];
120     if(refCount > 0 && (kForceLoad || !iMesh.second.geometry))
121     {
122       auto raw     = iMesh.first.LoadRaw(modelsPath);
123       iMesh.second = iMesh.first.Load(std::move(raw));
124     }
125     else if(!kKeepUnused && refCount == 0 && iMesh.second.geometry)
126     {
127       iMesh.second.geometry = Geometry();
128     }
129   }
130
131   const auto& refCountMaterials = refCounts[ResourceType::Material];
132   auto        imagesPath        = pathProvider(ResourceType::Material);
133   for(uint32_t i = 0, iEnd = refCountMaterials.Size(); i != iEnd; ++i)
134   {
135     auto  refCount  = refCountMaterials[i];
136     auto& iMaterial = mMaterials[i];
137     if(refCount > 0 && (kForceLoad || !iMaterial.second))
138     {
139       auto raw         = iMaterial.first.LoadRaw(imagesPath);
140       iMaterial.second = iMaterial.first.Load(mEnvironmentMaps, std::move(raw));
141     }
142     else if(!kKeepUnused && refCount == 0 && iMaterial.second)
143     {
144       iMaterial.second = TextureSet();
145     }
146   }
147 }
148
149 } // namespace SceneLoader
150 } // namespace Dali