1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 #ifndef DISPLAY_COLOR_LOADER_H
18 #define DISPLAY_COLOR_LOADER_H
19 
20 #include <dlfcn.h>
21 #include <gs101/displaycolor/displaycolor_gs101.h>
22 #include <log/log.h>
23 #include <string>
24 #include <vector>
25 
26 namespace gs101 {
27 
28 class DisplayColorLoader {
29     public:
DisplayColorLoader(const char * lib_name)30       DisplayColorLoader(const char *lib_name) {
31           lib_handle = dlopen(lib_name, RTLD_LAZY);
32 
33           if (lib_handle != nullptr) {
34               const displaycolor::DisplayColorIntfVer *(*get_version)();
35               get_version = (decltype(get_version))
36                                     dlsym(lib_handle, "GetInterfaceVersion");
37               if (get_version == nullptr) {
38                   ALOGE("%s: prebuilt lib is not versioned", __func__);
39               } else {
40                   auto intf_ver = get_version();
41 
42                   if (intf_ver != nullptr &&
43                       displaycolor::kInterfaceVersion.Compatible(*intf_ver)) {
44                       get_display_color_gs101 =
45                               (decltype(get_display_color_gs101))dlsym(lib_handle,
46                                                                        "GetDisplayColorGS101");
47 
48                       if (get_display_color_gs101 == nullptr) {
49                           ALOGE("%s: failed to get GetDisplayColorGS101\n", __func__);
50                       } else if (!(displaycolor::kInterfaceVersion == *intf_ver)) {
51                           ALOGW("%s: different hwc/displaycolor patch level %u.%u.%u/%u",
52                                 __func__,
53                                 intf_ver->major,
54                                 intf_ver->minor,
55                                 displaycolor::kInterfaceVersion.patch,
56                                 intf_ver->patch);
57                       }
58                   } else {
59                       if (intf_ver != nullptr) {
60                           ALOGE("%s: prebuilt lib version %u.%u.%u expected %u.%u.%u",
61                                 __func__,
62                                 intf_ver->major,
63                                 intf_ver->minor,
64                                 intf_ver->patch,
65                                 displaycolor::kInterfaceVersion.major,
66                                 displaycolor::kInterfaceVersion.minor,
67                                 displaycolor::kInterfaceVersion.patch);
68                       } else {
69                           ALOGE("%s: prebult lib get_version returns null", __func__);
70                       }
71                   }
72               }
73           } else {
74               ALOGE("%s: failed to load library %s\n", __func__, lib_name);
75               get_display_color_gs101 = nullptr;
76           }
77       }
78 
GetDisplayColorGS101(const std::vector<displaycolor::DisplayInfo> & display_info)79       displaycolor::IDisplayColorGS101 *GetDisplayColorGS101(
80               const std::vector<displaycolor::DisplayInfo> &display_info) {
81           if (get_display_color_gs101 != nullptr) {
82               return get_display_color_gs101(display_info);
83           }
84 
85           return nullptr;
86       }
87 
~DisplayColorLoader()88       ~DisplayColorLoader() {
89           if (lib_handle != nullptr) {
90               dlclose(lib_handle);
91           }
92       }
93 
94     private:
95       void *lib_handle;
96       displaycolor::IDisplayColorGS101 *(*get_display_color_gs101)(
97               const std::vector<displaycolor::DisplayInfo> &);
98 };
99 
100 }  // namespace gs101
101 
102 #endif //DISPLAY_COLOR_LOADER_H
103