(AutomatedTests) Fix build break
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / model-data-impl.cpp
1 /*
2  * Copyright (c) 2014 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/internal/event/modeling/model-data-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <stdio.h>
23 #include <algorithm>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/light.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/internal/event/common/thread-local-storage.h>
30 #include <dali/internal/event/images/resource-image-impl.h>
31 #include <dali/internal/event/modeling/entity-impl.h>
32 #include <dali/internal/event/modeling/material-impl.h>
33 #include <dali/internal/event/modeling/mesh-impl.h>
34 #include <dali/internal/event/modeling/model-archive.h>
35 #include <dali/internal/update/modeling/internal-mesh-data.h>
36
37 namespace
38 {
39 const unsigned int ModelDataFormatVersion = 0x10004;
40 }
41
42 namespace Dali
43 {
44
45 namespace Internal
46 {
47
48 using Dali::Vector4;
49 using namespace Serialize;
50
51 ModelData::ModelData()
52 : mRoot(),
53   mUnpacked( false )
54 {
55   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
56 }
57
58 ModelDataPtr ModelData::New(const std::string& name)
59 {
60   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
61
62   ModelDataPtr modelData(new ModelData());
63   modelData->SetName(name);
64   return modelData;
65 }
66
67 ModelData::~ModelData()
68 {
69   DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
70
71   mMeshes.Clear();
72   mMaterials.clear();
73   mAnimationMaps.clear();
74   mLights.clear();
75 }
76
77 void ModelData::SetName(const std::string& name)
78 {
79   mName = name;
80 }
81
82 const std::string& ModelData::GetName() const
83 {
84   return mName;
85 }
86
87 void ModelData::SetRootEntity(Dali::Entity root)
88 {
89   mRoot = root;
90 }
91
92 Dali::Entity ModelData::GetRootEntity() const
93 {
94   return mRoot;
95 }
96
97 void ModelData::AddMesh(const Dali::MeshData& mesh)
98 {
99   mMeshes.PushBack( new Dali::MeshData( mesh ) );
100 }
101
102 void ModelData::AddMeshTicket(ResourceTicketPtr ticket)
103 {
104   mMeshTickets.push_back(ticket);
105 }
106
107 const Dali::MeshData& ModelData::GetMesh(unsigned int index) const
108 {
109   DALI_ASSERT_ALWAYS( index < mMeshes.Size() && "Mesh index out of bounds" );
110   return *mMeshes[index];
111 }
112
113 Dali::MeshData& ModelData::GetMesh(unsigned int index)
114 {
115   DALI_ASSERT_ALWAYS( index < mMeshes.Size() && "Mesh index out of bounds" );
116   return *mMeshes[index];
117 }
118
119 const ResourceTicketPtr ModelData::GetMeshTicket(unsigned int index) const
120 {
121   DALI_ASSERT_ALWAYS( index < mMeshTickets.size() && "Mesh index out of bounds" );
122   return mMeshTickets[index];
123 }
124
125 unsigned int ModelData::NumberOfMeshes() const
126 {
127   return mMeshes.Size();
128 }
129
130 void ModelData::AddMaterial(Dali::Material material)
131 {
132   mMaterials.push_back(material);
133 }
134
135 Dali::Material ModelData::GetMaterial(unsigned int index) const
136 {
137   DALI_ASSERT_DEBUG(index < mMaterials.size());
138
139   return mMaterials[index];
140 }
141
142 unsigned int ModelData::NumberOfMaterials() const
143 {
144   return mMaterials.size();
145 }
146
147 ModelAnimationMapContainer& ModelData::GetAnimationMapContainer()
148 {
149   return mAnimationMaps;
150 }
151
152 const ModelAnimationMap* ModelData::GetAnimationMap (unsigned int index) const
153 {
154   const ModelAnimationMap* found(NULL);
155   if( index < mAnimationMaps.size() )
156   {
157     found = &mAnimationMaps[index];
158   }
159   return found;
160 }
161
162 const ModelAnimationMap* ModelData::GetAnimationMap (const std::string& name) const
163 {
164   const ModelAnimationMap* found(NULL);
165
166   for(ModelAnimationMapContainer::const_iterator iter=mAnimationMaps.begin();
167       iter != mAnimationMaps.end();
168       ++iter)
169   {
170     const ModelAnimationMap& animData = *iter;
171     if(animData.name == name)
172     {
173       found = &(*iter);
174       break;
175     }
176   }
177
178   return found;
179 }
180
181 bool ModelData::FindAnimation (const std::string& name, unsigned int& index) const
182 {
183   bool found = false;
184
185   index = 0;
186   for(ModelAnimationMapContainer::const_iterator iter=mAnimationMaps.begin();
187       iter != mAnimationMaps.end();
188       ++iter, ++index)
189   {
190     const ModelAnimationMap& animData(*iter);
191     if(animData.name == name)
192     {
193       found = true;
194       break;
195     }
196   }
197   return found;
198 }
199
200 float ModelData::GetAnimationDuration(size_t animationIndex) const
201 {
202   DALI_ASSERT_DEBUG (animationIndex < mAnimationMaps.size ());
203   const ModelAnimationMap& animData( mAnimationMaps[animationIndex] );
204   return animData.duration;
205 }
206
207 unsigned int ModelData::NumberOfAnimationMaps() const
208 {
209   return mAnimationMaps.size();
210 }
211
212 void ModelData::AddLight(Dali::Light light)
213 {
214   mLights.push_back(light);
215 }
216
217 Dali::Light ModelData::GetLight(unsigned int index) const
218 {
219   Dali::Light light;
220   if (index < mLights.size())
221   {
222     light = mLights[index];
223   }
224   return light;
225 }
226
227 unsigned int ModelData::NumberOfLights() const
228 {
229   return mLights.size();
230 }
231
232 void ModelData::Unpack( ResourceClient& resourceClient )
233 {
234   ResourcePolicy::DataRetention dataRetentionPolicy = resourceClient.GetResourceDataRetentionPolicy();
235   ResourcePolicy::Discardable discardable = (dataRetentionPolicy == ResourcePolicy::DALI_DISCARDS_ALL_DATA ) ? ResourcePolicy::DISCARD : ResourcePolicy::RETAIN;
236
237   bool scalable = false; /* scaling is transmitted through parent Node */
238
239   // Only unpack once
240   if ( !mUnpacked )
241   {
242     // Ensure that mesh tickets are in same order as the meshes they represent
243     for ( size_t meshIdx = 0; meshIdx < NumberOfMeshes(); meshIdx++ )
244     {
245       // Copy the mesh-data into an internal structure, and pass ownership to the resourceClient
246       OwnerPointer<MeshData> meshDataPtr( new MeshData( GetMesh( meshIdx ), discardable, scalable ));
247       ResourceTicketPtr meshTicket = resourceClient.AllocateMesh( meshDataPtr );
248
249       AddMeshTicket( meshTicket );
250     }
251
252     // TODO: Update to use image tickets directly. Would need access to ImageFactory.
253     for ( size_t materialIdx = 0; materialIdx < NumberOfMaterials(); materialIdx++ )
254     {
255       Dali::Material material = GetMaterial( materialIdx );
256       const std::string& diffuseFileName = material.GetDiffuseFileName();
257       if( ! diffuseFileName.empty() )
258       {
259         material.SetDiffuseTexture( Dali::ResourceImage::New( diffuseFileName ) );
260       }
261
262       const std::string& opacityFileName = material.GetOpacityTextureFileName();
263       if( ! opacityFileName.empty() )
264       {
265         material.SetOpacityTexture( Dali::ResourceImage::New( opacityFileName ) );
266       }
267
268       const std::string& normalMapFileName = material.GetNormalMapFileName();
269       if( ! normalMapFileName.empty() )
270       {
271         material.SetNormalMap( Dali::ResourceImage::New( normalMapFileName ) );
272       }
273     }
274
275     mUnpacked = true;
276   }
277 }
278
279 bool ModelData::Read(std::streambuf& buf)
280 {
281   InputArchive ar(buf, ModelDataFormatVersion);
282   ar >> *this;
283   return ar.GetResult();
284 }
285
286 bool ModelData::Write(std::streambuf& buf) const
287 {
288   OutputArchive ar(buf, ModelDataFormatVersion);
289   ar << *this;
290   return ar.GetResult();
291 }
292
293 } // namespace Internal
294
295 } // namespace Dali