blob: 8c8f55d3bae9589b3b62ddf3059b9e9e1399dc9a [file] [log] [blame]
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -08001#include "X11Support.h"
2
Joshua Duongef2bbc22022-10-05 11:59:15 -07003#include "aemu/base/SharedLibrary.h"
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -08004
5#define DEFINE_DUMMY_IMPL(rettype, funcname, args) \
6 static rettype dummy_##funcname args { \
7 return (rettype)0; \
8 } \
9
10LIST_XLIB_FUNCTYPES(DEFINE_DUMMY_IMPL)
11LIST_GLX_FUNCTYPES(DEFINE_DUMMY_IMPL)
12
13class X11FunctionGetter {
14 public:
15 X11FunctionGetter() :
Yahan Zhouc38135f2023-02-02 14:02:24 -080016 mX11Lib(android::base::SharedLibrary::open("libX11")) {
Bo Huf58fdca2023-05-01 06:59:23 -070017 if (!mX11Lib) {
18 fprintf(stderr, "WARNING: could not open libX11.so, try libX11.so.6\n");
19 mX11Lib = (android::base::SharedLibrary::open("libX11.so.6"));
20 if (!mX11Lib) {
21 fprintf(stderr, "ERROR: could not open libX11.so.6, give up\n");
22 return;
23 }
24 }
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080025
26#define X11_ASSIGN_DUMMY_IMPL(funcname) mApi.funcname = dummy_##funcname;
27
28 LIST_XLIB_FUNCS(X11_ASSIGN_DUMMY_IMPL)
29
30 if (!mX11Lib) return;
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080031
32#define X11_GET_FUNC(funcname) \
33 { \
34 auto f = mX11Lib->findSymbol(#funcname); \
35 if (f) mApi.funcname = (funcname##_t)f; \
36 } \
Yahan Zhouc38135f2023-02-02 14:02:24 -080037
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080038 LIST_XLIB_FUNCS(X11_GET_FUNC);
39
40 }
41
42 X11Api* getApi() { return &mApi; }
43 private:
44 android::base::SharedLibrary* mX11Lib;
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080045
46 X11Api mApi;
47};
48
49class GlxFunctionGetter {
50 public:
Yahan Zhoub4f524d2023-01-24 10:27:21 -080051 // Important: Use libGL.so.1 explicitly, because it will always link to
52 // the vendor-specific version of the library. libGL.so might in some
53 // cases, depending on bad ldconfig configurations, link to the wrapper
54 // lib that doesn't behave the same.
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080055 GlxFunctionGetter() :
Yahan Zhoub4f524d2023-01-24 10:27:21 -080056 mGlxLib(android::base::SharedLibrary::open("libGL.so.1")) {
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080057
58#define GLX_ASSIGN_DUMMY_IMPL(funcname) mApi.funcname = dummy_##funcname;
59
60 LIST_GLX_FUNCS(GLX_ASSIGN_DUMMY_IMPL)
61
62 if (!mGlxLib) return;
63
64#define GLX_GET_FUNC(funcname) \
65 { \
66 auto f = mGlxLib->findSymbol(#funcname); \
67 if (f) mApi.funcname = (funcname##_t)f; \
68 } \
69
70 LIST_GLX_FUNCS(GLX_GET_FUNC);
71
72 }
73
74 GlxApi* getApi() { return &mApi; }
75
76 private:
77 android::base::SharedLibrary* mGlxLib;
78
79 GlxApi mApi;
80};
81
Lingfeng Yangf64ede52020-11-04 16:26:28 -080082AEMU_EXPORT struct X11Api* getX11Api() {
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080083 static X11FunctionGetter* g = new X11FunctionGetter;
84 return g->getApi();
85}
86
Lingfeng Yangf64ede52020-11-04 16:26:28 -080087AEMU_EXPORT struct GlxApi* getGlxApi() {
Lingfeng Yanga2a3b0f2020-11-04 11:50:46 -080088 static GlxFunctionGetter* g = new GlxFunctionGetter;
89 return g->getApi();
90}