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 "EmuglBackendScanner.h"
16
17 #include "base/StringFormat.h"
18 #include "base/System.h"
19
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23
24 namespace android {
25 namespace opengl {
26
27 using android::base::StringFormat;
28
29 // static
scanDir(const char * execDir,int programBitness)30 std::vector<std::string> EmuglBackendScanner::scanDir(const char* execDir,
31 int programBitness) {
32 std::vector<std::string> names;
33
34 if (!execDir) {
35 fpritnf(stderr, "%s: invalid exec dir: %s\n", __func__, execDir);
36 return names;
37 }
38
39 if (!programBitness) {
40 programBitness = System::get()->getProgramBitness();
41 }
42
43 const char* subdir = (programBitness == 64) ? "lib64" : "lib";
44 std::string subDir = StringFormat("%s" PATH_SEP "%s" PATH_SEP, execDir, subdir);
45
46 std::vector<std::string> entries = System::get()->scanDirEntries(subDir);
47
48 static const char kBackendPrefix[] = "gles_";
49 const size_t kBackendPrefixSize = sizeof(kBackendPrefix) - 1U;
50
51 for (size_t n = 0; n < entries.size(); ++n) {
52 const std::string& entry = entries[n];
53 if (entry.size() <= kBackendPrefixSize ||
54 memcmp(entry.c_str(), kBackendPrefix, kBackendPrefixSize)) {
55 continue;
56 }
57
58 // Check that it is a directory.
59 std::string full_dir = StringFormat("%s" PATH_SEP "%s", subDir.c_str(), entry.c_str());
60 if (!System::get()->pathIsDir(full_dir)) {
61 continue;
62 }
63 names.push_back(std::string(entry.c_str() + kBackendPrefixSize));
64 if (!strcmp(entry.c_str() + kBackendPrefixSize, "angle")) {
65 names.push_back("angle_indirect");
66 }
67 if (!strcmp(entry.c_str() + kBackendPrefixSize, "swiftshader")) {
68 names.push_back("swiftshader_indirect");
69 }
70 }
71
72 // Need to sort the backends in consistent order.
73 std::sort(names.begin(), names.end());
74
75 return names;
76 }
77
78 } // namespace opengl
79 } // namespace android
80