Emscripten workarounds and llvm syntax fixes
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / model-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/modeling/model-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <cstdio>
22
23 // INTERNAL INCLUDES
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/resource-cache.h>
26 #include <dali/integration-api/platform-abstraction.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/modeling/model-factory.h>
29 #include <dali/internal/event/modeling/model-logger.h>
30 #include <dali/internal/event/modeling/model-data-impl.h>
31 #include <dali/public-api/modeling/model-animation-map.h>
32
33 #include <dali/internal/event/resources/resource-client.h>
34 #include <dali/internal/update/resources/resource-manager.h>
35
36 namespace Dali
37 {
38
39 using Internal::ModelDataPtr;
40 using Integration::ModelResourceType;
41 using Integration::PlatformAbstraction;
42
43 namespace Internal
44 {
45
46 using Dali::Vector4;
47
48 ModelPtr Model::New(const std::string& name)
49 {
50   ModelPtr model(new Model(name));
51
52   model->RegisterObject();
53
54   return model;
55 }
56
57 Model::Model(const std::string& name)
58 {
59   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
60
61   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
62   mResourceClient = &tls.GetResourceClient();
63
64   ModelFactory& modelFactory = tls.GetModelFactory();
65
66   mTicket = modelFactory.Load(name);
67   mTicket->AddObserver(*this);
68 }
69
70 Model::~Model()
71 {
72   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
73   mTicket->RemoveObserver(*this);
74
75   UnregisterObject();
76 }
77
78 void Model::ResourceLoadingFailed(const ResourceTicket& ticket)
79 {
80   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
81
82   mLoadingFinishedV2.Emit( Dali::Model( this ) );
83 }
84
85 void Model::ResourceLoadingSucceeded(const ResourceTicket& ticket)
86 {
87   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
88
89   // Generate resource tickets for meshes
90   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
91   ResourceManager& resourceManager = tls.GetResourceManager();
92   ModelDataPtr modelData = resourceManager.GetModelData( GetResourceId() );
93   modelData->Unpack( *mResourceClient );
94
95   mLoadingFinishedV2.Emit( Dali::Model( this ) );
96 }
97
98 void Model::ResourceUploaded(const ResourceTicket& ticket)
99 {
100   // do nothing - not a GL resource
101 }
102
103 void Model::ResourceSavingFailed(const ResourceTicket& ticket)
104 {
105   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
106
107   mSavingFinishedV2.Emit( Dali::Model( this ), false );
108 }
109
110 void Model::ResourceSavingSucceeded(const ResourceTicket& ticket)
111 {
112   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
113
114   mSavingFinishedV2.Emit( Dali::Model( this ), true );
115 }
116
117 ResourceId Model::GetResourceId() const
118 {
119   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
120   DALI_ASSERT_DEBUG(mTicket);
121
122   return mTicket->GetId();
123 }
124
125 /********************************************************************************
126  * Write the model data to standard output
127  */
128 void Model::Write()
129 {
130   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
131   ResourceManager& resourceManager = tls.GetResourceManager();
132   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
133
134   if(modelData != 0)
135   {
136     ModelLogger modelLogger(*(modelData.Get()));
137     modelLogger.mWriteVertices = false; // TODO - add a public traits class for model logger
138     modelLogger.Write();
139   }
140   else
141   {
142 #ifndef EMSCRIPTEN
143     fprintf(stderr, "%s: Model not loaded yet\n", __PRETTY_FUNCTION__);
144 #else
145     // printf to stderr doesnt always show in browser console window
146     printf("%s: Model not loaded yet\n", __PRETTY_FUNCTION__);
147 #endif
148   }
149 }
150
151 /********************************************************************************
152  * Write the model data to a url
153  */
154 void Model::Save(const std::string& url)
155 {
156   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
157   ResourceManager& resourceManager = tls.GetResourceManager();
158   if( resourceManager.GetModelData(GetResourceId()) )
159   {
160     mResourceClient->SaveResource(mTicket, url);
161   }
162 }
163
164 /********************************************************************************
165  *
166  */
167 unsigned int Model::NumberOfAnimations() const
168 {
169   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
170
171   unsigned int numAnimations = 0;
172   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
173   ResourceManager& resourceManager = tls.GetResourceManager();
174   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
175
176   if (modelData)
177   {
178     numAnimations = modelData->NumberOfAnimationMaps();
179   }
180
181   return numAnimations;
182 }
183
184 bool Model::FindAnimation(const std::string& animationName, unsigned int & animationIndex)
185 {
186   bool found = false;
187
188   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
189   ResourceManager& resourceManager = tls.GetResourceManager();
190   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
191
192   if (modelData)
193   {
194     found = modelData->FindAnimation(animationName, animationIndex);
195   }
196   return found;
197 }
198
199 /********************************************************************************
200  * Return the model data, if it's finished loading.
201  */
202 ModelDataPtr Model::GetModelData() const
203 {
204   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
205
206   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
207   ResourceManager& resourceManager = tls.GetResourceManager();
208   ModelDataPtr modelData = resourceManager.GetModelData(GetResourceId());
209   return modelData;
210 }
211
212 } // namespace Internal
213 } // namespace Dali