support image loader extension
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / image-loader-plugin-proxy.cpp
1 /*
2  * Copyright (c) 2018 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/imaging/common/image-loader-plugin-proxy.h>
20
21 // EXTERNAL INCLUDES
22 #include <dlfcn.h>
23 #include <dali/integration-api/debug.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 namespace ImageLoaderPluginProxy
35 {
36
37
38 static const char * DEFAULT_OBJECT_NAME( "libdali-image-loader-plugin.so" );
39
40 static bool mInitializeAttempted = false;
41 static void* mLibHandle = NULL;
42 static CreateImageLoaderPlugin* mCreatePluginFunctionPtr = NULL;
43 static DestroyImageLoaderPlugin* mDestroyImageLoaderPluginPtr = NULL;
44 static Dali::ImageLoaderPlugin* mImageLoaderPlugin = NULL;
45
46 void Initialize()
47 {
48   // Only attempt to load dll once
49   char* error = NULL;
50   if ( !mInitializeAttempted )
51   {
52     mInitializeAttempted = true;
53     mLibHandle = dlopen( DEFAULT_OBJECT_NAME, RTLD_LAZY );
54     error = dlerror();
55     if( !mLibHandle )
56     {
57       DALI_LOG_ERROR( "Cannot load dali image loading plugin library error: %s\n", error );
58       return;
59     }
60
61     // load plugin
62     mCreatePluginFunctionPtr = reinterpret_cast<CreateImageLoaderPlugin*>( dlsym( mLibHandle, "CreateImageLoaderPlugin" ) );
63     error = dlerror();
64     if( !mCreatePluginFunctionPtr )
65     {
66       DALI_LOG_ERROR("Cannot load symbol CreateImageLoaderPlugin(): %s\n", error );
67       return;
68     }
69
70     mDestroyImageLoaderPluginPtr = reinterpret_cast<DestroyImageLoaderPlugin*>( dlsym( mLibHandle, "DestroyImageLoaderPlugin" ) );
71     error = dlerror();
72     if( !mDestroyImageLoaderPluginPtr )
73     {
74       DALI_LOG_ERROR("Cannot load symbol DestroyImageLoaderPlugin(): %s\n", error );
75       return;
76     }
77
78
79     mImageLoaderPlugin = mCreatePluginFunctionPtr();
80     error = dlerror();
81     if( !mImageLoaderPlugin )
82     {
83       DALI_LOG_ERROR("Call to function CreateImageLoaderPlugin() failed : %s\n", error );
84       return;
85     }
86   }
87 }
88
89 void Destroy()
90 {
91   if( mImageLoaderPlugin && mDestroyImageLoaderPluginPtr )
92   {
93     mDestroyImageLoaderPluginPtr( mImageLoaderPlugin );
94     mImageLoaderPlugin = NULL;
95   }
96 }
97
98 const ImageLoader::BitmapLoader* BitmapLoaderLookup( const std::string& filename )
99 {
100   if( mImageLoaderPlugin )
101   {
102     const ImageLoader::BitmapLoader* data = mImageLoaderPlugin->BitmapLoaderLookup( filename );
103     return data;
104   }
105   return NULL;
106 }
107
108 } // namespace ImageLoaderPluginProxy
109
110
111 } // namespace Adaptor
112
113 } // namespace Internal
114
115 } // namespace Dali