OSDN Git Service

Enable native bridge
[android-x86/device-generic-common.git] / nativebridge / src / libnb.cpp
1 /*
2  * Copyright (C) 2015 The Android-x86 Open Source Project
3  *
4  * by Chih-Wei Huang <cwhuang@linux.org.tw>
5  *
6  * Licensed under the GNU General Public License Version 2 or later.
7  * You may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.gnu.org/licenses/gpl.html
11  *
12  */
13
14 #define LOG_TAG "libnb"
15
16 #include <dlfcn.h>
17 #include <cutils/log.h>
18 #include "nativebridge/native_bridge.h"
19
20 namespace android {
21
22 static void *native_handle = nullptr;
23
24 static NativeBridgeCallbacks *get_callbacks()
25 {
26     static NativeBridgeCallbacks *callbacks = nullptr;
27
28     if (!callbacks) {
29         const char *libnb = "/system/"
30 #ifdef __LP64__
31                 "lib64/arm64/"
32 #else
33                 "lib/arm/"
34 #endif
35                 "libhoudini.so";
36         if (!native_handle) {
37             native_handle = dlopen(libnb, RTLD_LAZY);
38             if (!native_handle) {
39                 ALOGE("Unable to open %s", libnb);
40                 return nullptr;
41             }
42         }
43         callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle, "NativeBridgeItf"));
44     }
45     return callbacks;
46 }
47
48 // NativeBridgeCallbacks implementations
49 static bool native_bridge2_initialize(const NativeBridgeRuntimeCallbacks *art_cbs,
50                                       const char *app_code_cache_dir,
51                                       const char *isa)
52 {
53     ALOGV("enter native_bridge2_initialize %s %s", app_code_cache_dir, isa);
54     NativeBridgeCallbacks *cb = get_callbacks();
55     return cb ? cb->initialize(art_cbs, app_code_cache_dir, isa) : false;
56 }
57
58 static void *native_bridge2_loadLibrary(const char *libpath, int flag)
59 {
60     ALOGV("enter native_bridge2_loadLibrary %s", libpath);
61     NativeBridgeCallbacks *cb = get_callbacks();
62     return cb ? cb->loadLibrary(libpath, flag) : nullptr;
63 }
64
65 static void *native_bridge2_getTrampoline(void *handle, const char *name,
66                                           const char* shorty, uint32_t len)
67 {
68     ALOGV("enter native_bridge2_getTrampoline %s", name);
69     NativeBridgeCallbacks *cb = get_callbacks();
70     return cb ? cb->getTrampoline(handle, name, shorty, len) : nullptr;
71 }
72
73 static bool native_bridge2_isSupported(const char *libpath)
74 {
75     ALOGV("enter native_bridge2_isSupported %s", libpath);
76     NativeBridgeCallbacks *cb = get_callbacks();
77     return cb ? cb->isSupported(libpath) : false;
78 }
79
80 static const struct NativeBridgeRuntimeValues *native_bridge2_getAppEnv(const char *abi)
81 {
82     ALOGV("enter native_bridge2_getAppEnv %s", abi);
83     NativeBridgeCallbacks *cb = get_callbacks();
84     return cb ? cb->getAppEnv(abi) : nullptr;
85 }
86
87 static bool native_bridge2_is_compatible_compatible_with(uint32_t version)
88 {
89     // For testing, allow 1 and 2, but disallow 3+.
90     return version <= 2;
91 }
92
93 static NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal)
94 {
95     ALOGV("enter native_bridge2_getAppEnv %d", signal);
96     NativeBridgeCallbacks *cb = get_callbacks();
97     return cb ? cb->getSignalHandler(signal) : nullptr;
98 }
99
100 static void __attribute__ ((destructor)) on_dlclose()
101 {
102     if (native_handle) {
103         dlclose(native_handle);
104         native_handle = nullptr;
105     }
106 }
107
108 extern "C" {
109
110 NativeBridgeCallbacks NativeBridgeItf = {
111     version: 2,
112     initialize: &native_bridge2_initialize,
113     loadLibrary: &native_bridge2_loadLibrary,
114     getTrampoline: &native_bridge2_getTrampoline,
115     isSupported: &native_bridge2_isSupported,
116     getAppEnv: &native_bridge2_getAppEnv,
117     isCompatibleWith: &native_bridge2_is_compatible_compatible_with,
118     getSignalHandler: &native_bridge2_get_signal_handler,
119 };
120
121 } // extern "C"
122 } // namespace android