| 1 |
/* |
| 2 |
* Copyright (C) 2018 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 |
#include <dlfcn.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#include <utils/Log.h> |
| 22 |
|
| 23 |
namespace bluevk { |
| 24 |
|
| 25 |
static void* module = nullptr; |
| 26 |
|
| 27 |
// Determines the file path to the Vulkan library and calls dlopen on it. |
| 28 |
// Returns false on failure. |
| 29 |
bool loadLibrary() { |
| 30 |
const char* path = nullptr; |
| 31 |
|
| 32 |
// For security, consult an environment variable only if Filament is built with this #define |
| 33 |
// enabled. Our weekly GitHub releases do not have this enabled. |
| 34 |
#ifdef FILAMENT_VKLIBRARY_USE_ENV |
| 35 |
path = getenv("FILAMENT_VKLIBRARY_PATH"); |
| 36 |
#endif |
| 37 |
|
| 38 |
// If the environment variable is not set, fall back to a config-specified path, which is either |
| 39 |
// a custom path (common for SwiftShader), or "libvulkan.so" (common for Android). |
| 40 |
if (path == nullptr) { |
| 41 |
#ifdef FILAMENT_VKLIBRARY_PATH |
| 42 |
path = FILAMENT_VKLIBRARY_PATH; |
| 43 |
#elif defined(__ANDROID__) |
| 44 |
path = "libvulkan.so"; |
| 45 |
#elif defined(__linux__) |
| 46 |
path = "libvulkan.so.1"; |
| 47 |
#else |
| 48 |
#error "This file should only be compiled for Android or Linux" |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
module = dlopen(path, RTLD_NOW | RTLD_LOCAL); |
| 53 |
if (module == nullptr) { |
| 54 |
utils::slog.e << "Unable to load Vulkan from " << path << utils::io::endl; |
| 55 |
} |
| 56 |
return module != nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
void* getInstanceProcAddr() { |
| 60 |
return dlsym(module, "vkGetInstanceProcAddr"); |
| 61 |
} |
| 62 |
|
| 63 |
} // namespace bluevk |