1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "host-common/opengl/EmuglBackendList.h"
16
17 #include "aemu/base/StringFormat.h"
18 #include "aemu/base/system/System.h"
19 #include "aemu/base/files/PathUtils.h"
20
21 #define DEBUG 0
22
23 #if DEBUG
24 # include <stdio.h>
25 # define D(...) printf(__VA_ARGS__)
26 #else
27 # define D(...) ((void)0)
28 #endif
29
30 namespace android {
31 namespace opengl {
32
EmuglBackendList(int programBitness,const std::vector<std::string> & names)33 EmuglBackendList::EmuglBackendList(int programBitness,
34 const std::vector<std::string>& names) :
35 mDefaultName("auto"), mNames(names), mProgramBitness(programBitness) { }
36
contains(const char * name) const37 bool EmuglBackendList::contains(const char* name) const {
38 for (size_t n = 0; n < mNames.size(); ++n) {
39 if (mNames[n] == name) {
40 return true;
41 }
42 }
43 return false;
44 }
45
getLibDirPath(const char * name)46 std::string EmuglBackendList::getLibDirPath(const char* name) {
47 // remove the "_indirect" suffix
48 std::string suffix("_indirect");
49 std::string nameNoSuffix(name);
50 int nameNoSuffixLen = (int)nameNoSuffix.size() - (int)suffix.size();
51 if (nameNoSuffixLen > 0 &&
52 suffix == nameNoSuffix.c_str() + nameNoSuffixLen) {
53 nameNoSuffix.erase(nameNoSuffixLen);
54 }
55 return android::base::pj({mExecDir, "lib64", std::string("gles_%s") + nameNoSuffix});
56 }
57
58 #ifdef _WIN32
59 static const char kLibSuffix[] = ".dll";
60 #elif defined(__APPLE__)
61 static const char kLibSuffix[] = ".dylib";
62 #else
63 static const char kLibSuffix[] = ".so";
64 #endif
65
getBackendLibPath(const char * name,Library library,std::string * libPath)66 bool EmuglBackendList::getBackendLibPath(const char* name,
67 Library library,
68 std::string* libPath) {
69
70 const char* libraryName = NULL;
71 if (library == LIBRARY_EGL) {
72 libraryName = "EGL";
73 } else if (library == LIBRARY_GLESv1) {
74 libraryName = "GLES_CM";
75 } else if (library == LIBRARY_GLESv2) {
76 libraryName = "GLESv2";
77 } else {
78 // Should not happen.
79 D("%s: Invalid library type: %d\n", __FUNCTION__, library);
80 return false;
81 }
82
83 std::string path = android::base::pj({
84 getLibDirPath(name), std::string("lib") + libraryName + kLibSuffix});
85
86 *libPath = path;
87 return true;
88 }
89
90 } // namespace opengl
91 } // namespace android
92