Removed legacy resource tracking / logging
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / assimp-proxy.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 #include "assimp-proxy.h"
17
18 #include <stdio.h>
19 #include <dlfcn.h>
20
21 #include <dali/integration-api/debug.h>
22
23
24 namespace Dali
25 {
26 namespace SlpPlatform
27 {
28
29 // Private typedefs for function pointers returned from dlsym().
30
31 typedef const struct aiScene* PFImportFile(const char*, size_t);
32 typedef void                  PFReleaseImport(const aiScene*);
33 typedef const char*           PFGetErrorString();
34
35 typedef enum aiReturn PFGetMaterialColor(
36   const C_STRUCT aiMaterial* pMaterial,
37   const char*                pKey,
38   unsigned int               type,
39   unsigned int               index,
40   C_STRUCT aiColor4D*        pOut);
41
42 typedef enum aiReturn PFGetMaterialIntegerArray(
43   const C_STRUCT aiMaterial* pMaterial,
44   const char*                pKey,
45   unsigned int               type,
46   unsigned int               index,
47   int*                       pOut,
48   unsigned int*              pMax);
49
50 typedef enum aiReturn PFGetMaterialFloatArray(
51   const C_STRUCT aiMaterial* pMaterial,
52   const char*                pKey,
53   unsigned int               type,
54   unsigned int               index,
55   float*                     pOut,
56   unsigned int*              pMax);
57
58 typedef enum aiReturn PFGetMaterialString(
59   const C_STRUCT aiMaterial* pMaterial,
60   const char*                pKey,
61   unsigned int               type,
62   unsigned int               index,
63   C_STRUCT aiString*         pOut);
64
65
66 /**
67  * Class to proxy the Assimp importer. This will allow us to dynamically load the assimp
68  * library when required.
69  */
70 AssimpProxy::AssimpProxy()
71   : mLibHandle(NULL)
72 {
73   mLibHandle = dlopen("libassimp.so", RTLD_LAZY);
74   if(!mLibHandle)
75   {
76     fprintf(stderr, "Cannot load assimp library: %s\n", dlerror());
77     mLibHandle = NULL;
78   }
79   else
80   {
81     // reset errors
82     dlerror();
83   }
84 }
85
86 AssimpProxy::~AssimpProxy()
87 {
88   if(mLibHandle != NULL)
89   {
90     if(dlclose(mLibHandle))
91     {
92       fprintf(stderr, "Error closing assimp library: %s\n", dlerror());
93     }
94   }
95 }
96
97
98 const struct aiScene* AssimpProxy::ImportFile(std::string fileName, unsigned int postProcessFlags)
99 {
100   const struct aiScene* scene = NULL;
101   PFImportFile* aiImportFile = (PFImportFile*) dlsym(mLibHandle, "aiImportFile");
102   if(aiImportFile == NULL)
103   {
104     fprintf(stderr, "Cannot get aiImportFile symbol from library: %s\n", dlerror());
105   }
106   else
107   {
108     dlerror();
109     scene = aiImportFile(fileName.c_str(), postProcessFlags);
110   }
111   return scene;
112 }
113
114
115 void AssimpProxy::ReleaseImport(const aiScene* scene)
116 {
117   PFReleaseImport* aiReleaseImport = (PFReleaseImport*) dlsym(mLibHandle, "aiReleaseImport");
118   if(aiReleaseImport == NULL)
119   {
120     fprintf(stderr, "Cannot get aiReleaseImport symbol from library: %s\n", dlerror());
121   }
122   else
123   {
124     dlerror();
125     aiReleaseImport(scene);
126   }
127 }
128
129
130 const char* AssimpProxy::GetErrorString()
131 {
132   const char* errorString=NULL;
133
134   PFGetErrorString* aiGetErrorString = (PFGetErrorString*) dlsym(mLibHandle, "aiGetErrorString");
135   if(aiGetErrorString == NULL)
136   {
137     fprintf(stderr, "Cannot get aiGetErrorString symbol from library: %s\n", dlerror());
138   }
139   else
140   {
141     dlerror();
142     errorString = aiGetErrorString();
143   }
144
145   return errorString;
146 }
147
148
149 enum aiReturn AssimpProxy::GetMaterialColor(
150   const C_STRUCT aiMaterial* material,
151   const char*                key,
152   unsigned int               type,
153   unsigned int               index,
154   C_STRUCT aiColor4D*        data)
155 {
156   enum aiReturn status = aiReturn_FAILURE;
157
158   PFGetMaterialColor* aiGetMaterialColor = (PFGetMaterialColor*) dlsym(mLibHandle, "aiGetMaterialColor");
159   if(aiGetMaterialColor == NULL)
160   {
161     fprintf(stderr, "Cannot get aiGetMaterialColor symbol from library: %s\n", dlerror());
162   }
163   else
164   {
165     dlerror();
166     status = aiGetMaterialColor(material, key, type, index, data);
167   }
168
169   return status;
170 }
171
172
173 enum aiReturn AssimpProxy::GetMaterialInteger(
174   const C_STRUCT aiMaterial* material,
175   const char*                key,
176   unsigned int               type,
177   unsigned int               index,
178   int*                       data)
179 {
180   enum aiReturn status = aiReturn_FAILURE;
181
182   PFGetMaterialIntegerArray* aiGetMaterialIntegerArray = (PFGetMaterialIntegerArray*) dlsym(mLibHandle, "aiGetMaterialIntegerArray");
183   if(aiGetMaterialIntegerArray == NULL)
184   {
185     fprintf(stderr, "Cannot get aiGetMaterialIntegerArray symbol from library: %s\n", dlerror());
186   }
187   else
188   {
189     dlerror();
190     status = aiGetMaterialIntegerArray(material, key, type, index, data, NULL);
191   }
192
193   return status;
194 }
195
196 enum aiReturn AssimpProxy::GetMaterialFloat(
197   const C_STRUCT aiMaterial* material,
198   const char*                key,
199   unsigned int               type,
200   unsigned int               index,
201   float*                     data)
202 {
203   enum aiReturn status = aiReturn_FAILURE;
204
205   PFGetMaterialFloatArray* aiGetMaterialFloatArray = (PFGetMaterialFloatArray*) dlsym(mLibHandle, "aiGetMaterialFloatArray");
206   if(aiGetMaterialFloatArray == NULL)
207   {
208     fprintf(stderr, "Cannot get aiGetMaterialFloatArray symbol from library: %s\n", dlerror());
209   }
210   else
211   {
212     dlerror();
213     status = aiGetMaterialFloatArray(material, key, type, index, data, NULL);
214   }
215
216   return status;
217 }
218
219
220 enum aiReturn AssimpProxy::GetMaterialString(
221   const C_STRUCT aiMaterial* material,
222   const char*                key,
223   unsigned int               type,
224   unsigned int               index,
225   C_STRUCT aiString*         data)
226 {
227   enum aiReturn status = aiReturn_FAILURE;
228
229   PFGetMaterialString* aiGetMaterialString = (PFGetMaterialString*) dlsym(mLibHandle, "aiGetMaterialString");
230   if(aiGetMaterialString == NULL)
231   {
232     fprintf(stderr, "Cannot get aiGetMaterialString symbol from library: %s\n", dlerror());
233   }
234   else
235   {
236     dlerror();
237     status = aiGetMaterialString(material, key, type, index, data);
238   }
239
240   return status;
241 }
242
243
244
245 AssimpScene::AssimpScene(AssimpProxy* importer,
246                          std::string  fileName,
247                          unsigned int postProcessFlags)
248   : mModelImporter(importer)
249 {
250   mScene = importer->ImportFile(fileName, postProcessFlags);
251   if(!mScene)
252   {
253     DALI_LOG_ERROR("%s", importer->GetErrorString());
254   }
255 }
256
257 AssimpScene::~AssimpScene()
258 {
259   // Ensure the imported model data is released
260   mModelImporter->ReleaseImport(mScene);
261 }
262
263 const aiScene* AssimpScene::GetScene()
264 {
265   return mScene;
266 }
267
268
269 }
270 }
271