Support *.pkm format file decode
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / image-loader-plugin-proxy.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 // CLASS HEADER
19 #include <dali/internal/imaging/common/image-loader-plugin-proxy.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dlfcn.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 namespace ImageLoaderPluginProxy
32 {
33 static const char* DEFAULT_OBJECT_NAME("libdali2-image-loader-plugin.so");
34
35 static bool                      mInitializeAttempted         = false;
36 static void*                     mLibHandle                   = NULL;
37 static CreateImageLoaderPlugin*  mCreatePluginFunctionPtr     = NULL;
38 static DestroyImageLoaderPlugin* mDestroyImageLoaderPluginPtr = NULL;
39 static Dali::ImageLoaderPlugin*  mImageLoaderPlugin           = NULL;
40
41 #if defined(DEBUG_ENABLED)
42 /**
43  * Disable logging of image loader plugin proxy or make it verbose from the commandline
44  * as follows (e.g., for dali demo app):
45  * <code>
46  * LOG_IMAGE_LOADER_PLUGIN=0 dali-demo #< off
47  * LOG_IMAGE_LOADER_PLUGIN=3 dali-demo #< on, verbose
48  * </code>
49  */
50 Debug::Filter* gImageLoaderPluginLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_IMAGE_LOADER_PLUGIN");
51 #endif
52
53 void Initialize()
54 {
55   // Only attempt to load dll once
56   char* error = NULL;
57   if(!mInitializeAttempted)
58   {
59     mInitializeAttempted = true;
60     mLibHandle           = dlopen(DEFAULT_OBJECT_NAME, RTLD_LAZY);
61     error                = dlerror();
62     if(!mLibHandle)
63     {
64       DALI_LOG_INFO(gImageLoaderPluginLogFilter, Dali::Integration::Log::Verbose, "Cannot load dali image loading plugin library error: %s\n", error);
65       return;
66     }
67
68     // load plugin
69     mCreatePluginFunctionPtr = reinterpret_cast<CreateImageLoaderPlugin*>(dlsym(mLibHandle, "CreateImageLoaderPlugin"));
70     error                    = dlerror();
71     if(!mCreatePluginFunctionPtr)
72     {
73       DALI_LOG_ERROR("Cannot load symbol CreateImageLoaderPlugin(): %s\n", error);
74       return;
75     }
76
77     mDestroyImageLoaderPluginPtr = reinterpret_cast<DestroyImageLoaderPlugin*>(dlsym(mLibHandle, "DestroyImageLoaderPlugin"));
78     error                        = dlerror();
79     if(!mDestroyImageLoaderPluginPtr)
80     {
81       DALI_LOG_ERROR("Cannot load symbol DestroyImageLoaderPlugin(): %s\n", error);
82       return;
83     }
84
85     mImageLoaderPlugin = mCreatePluginFunctionPtr();
86     error              = dlerror();
87     if(!mImageLoaderPlugin)
88     {
89       DALI_LOG_ERROR("Call to function CreateImageLoaderPlugin() failed : %s\n", error);
90       return;
91     }
92   }
93 }
94
95 void Destroy()
96 {
97   if(mImageLoaderPlugin && mDestroyImageLoaderPluginPtr)
98   {
99     mDestroyImageLoaderPluginPtr(mImageLoaderPlugin);
100     mImageLoaderPlugin = NULL;
101   }
102 }
103
104 const ImageLoader::BitmapLoader* BitmapLoaderLookup(const std::string& filename)
105 {
106   if(mImageLoaderPlugin)
107   {
108     const ImageLoader::BitmapLoader* data = mImageLoaderPlugin->BitmapLoaderLookup(filename);
109     return data;
110   }
111   return NULL;
112 }
113
114 } // namespace ImageLoaderPluginProxy
115
116 } // namespace Adaptor
117
118 } // namespace Internal
119
120 } // namespace Dali