MangoHud/src/loaders/loader_glx.cpp

149 lines
3.9 KiB
C++
Raw Normal View History

2020-04-01 12:20:22 +00:00
#include <iostream>
2021-07-16 00:28:46 +00:00
#include <spdlog/spdlog.h>
#include "real_dlsym.h"
#include "loaders/loader_glx.h"
2020-03-10 05:19:18 +00:00
2020-04-01 12:20:22 +00:00
glx_loader::glx_loader() : loaded_(false) {
2020-03-10 05:19:18 +00:00
}
2020-04-01 12:20:22 +00:00
glx_loader::~glx_loader() {
2020-03-10 05:19:18 +00:00
CleanUp(loaded_);
}
2020-04-01 12:20:22 +00:00
bool glx_loader::Load() {
2020-03-10 05:19:18 +00:00
if (loaded_) {
return true;
}
2020-04-01 12:20:22 +00:00
// Force load libGL
void *handle = nullptr;
#ifndef NDEBUG
// Use apitrace's glxtrace.so for debugging
// Assumes glxtrace.so lives outside of usual library paths and is only preloaded
handle = real_dlopen("glxtrace.so", RTLD_LAZY);
#endif
if (!handle)
handle = real_dlopen("libGL.so.1", RTLD_LAZY);
2020-04-01 12:20:22 +00:00
if (!handle) {
SPDLOG_ERROR("Failed to open " MANGOHUD_ARCH " libGL.so.1: {}", dlerror());
2020-04-01 12:20:22 +00:00
return false;
2020-03-10 05:19:18 +00:00
}
2020-04-01 12:20:22 +00:00
GetProcAddress =
reinterpret_cast<decltype(this->GetProcAddress)>(
real_dlsym(handle, "glXGetProcAddress"));
2020-03-10 05:19:18 +00:00
2020-04-01 12:20:22 +00:00
GetProcAddressARB =
reinterpret_cast<decltype(this->GetProcAddressARB)>(
real_dlsym(handle, "glXGetProcAddressARB"));
2020-03-10 05:19:18 +00:00
2020-04-01 12:20:22 +00:00
if (!GetProcAddress) {
2020-03-10 05:19:18 +00:00
CleanUp(true);
return false;
}
2020-04-01 12:20:22 +00:00
CreateContext =
reinterpret_cast<decltype(this->CreateContext)>(
GetProcAddress((const unsigned char *)"glXCreateContext"));
if (!CreateContext) {
2020-03-10 05:19:18 +00:00
CleanUp(true);
return false;
}
CreateContextAttribs =
reinterpret_cast<decltype(this->CreateContextAttribs)>(
GetProcAddress((const unsigned char *)"glXCreateContextAttribs"));
// if (!CreateContextAttribs) {
// CleanUp(true);
// return false;
// }
CreateContextAttribsARB =
reinterpret_cast<decltype(this->CreateContextAttribsARB)>(
GetProcAddress((const unsigned char *)"glXCreateContextAttribsARB"));
// if (!CreateContextAttribsARB) {
// CleanUp(true);
// return false;
// }
2020-04-01 12:20:22 +00:00
DestroyContext =
reinterpret_cast<decltype(this->DestroyContext)>(
GetProcAddress((const unsigned char *)"glXDestroyContext"));
if (!DestroyContext) {
2020-03-10 05:19:18 +00:00
CleanUp(true);
return false;
}
2020-04-01 12:20:22 +00:00
GetCurrentContext =
reinterpret_cast<decltype(this->GetCurrentContext)>(
GetProcAddress((const unsigned char *)"glXGetCurrentContext"));
if (!GetCurrentContext) {
CleanUp(true);
return false;
}
2020-04-01 12:20:22 +00:00
SwapBuffers =
reinterpret_cast<decltype(this->SwapBuffers)>(
GetProcAddress((const unsigned char *)"glXSwapBuffers"));
if (!SwapBuffers) {
2020-03-10 05:19:18 +00:00
CleanUp(true);
return false;
}
SwapBuffersMscOML =
reinterpret_cast<decltype(this->SwapBuffersMscOML)>(
GetProcAddress((const unsigned char *)"glXSwapBuffersMscOML"));
/*if (!SwapBuffersMscOML) {
CleanUp(true);
return false;
}*/
2020-04-01 12:20:22 +00:00
SwapIntervalEXT =
reinterpret_cast<decltype(this->SwapIntervalEXT)>(
GetProcAddress((const unsigned char *)"glXSwapIntervalEXT"));
2020-03-10 05:19:18 +00:00
2020-04-01 12:20:22 +00:00
SwapIntervalSGI =
reinterpret_cast<decltype(this->SwapIntervalSGI)>(
GetProcAddress((const unsigned char *)"glXSwapIntervalSGI"));
2020-03-10 05:19:18 +00:00
2020-04-01 12:20:22 +00:00
SwapIntervalMESA =
reinterpret_cast<decltype(this->SwapIntervalMESA)>(
GetProcAddress((const unsigned char *)"glXSwapIntervalMESA"));
2020-03-10 05:19:18 +00:00
2020-04-01 12:20:22 +00:00
GetSwapIntervalMESA =
reinterpret_cast<decltype(this->GetSwapIntervalMESA)>(
GetProcAddress((const unsigned char *)"glXGetSwapIntervalMESA"));
2020-04-01 12:20:22 +00:00
QueryDrawable =
reinterpret_cast<decltype(this->QueryDrawable)>(
GetProcAddress((const unsigned char *)"glXQueryDrawable"));
2020-04-01 12:20:22 +00:00
MakeCurrent =
reinterpret_cast<decltype(this->MakeCurrent)>(
GetProcAddress((const unsigned char *)"glXMakeCurrent"));
if (!MakeCurrent) {
2020-03-10 05:19:18 +00:00
CleanUp(true);
return false;
}
loaded_ = true;
return true;
}
2020-04-01 12:20:22 +00:00
void glx_loader::CleanUp(bool unload) {
2020-03-10 05:19:18 +00:00
loaded_ = false;
2020-04-01 12:20:22 +00:00
GetProcAddress = nullptr;
GetProcAddressARB = nullptr;
CreateContext = nullptr;
DestroyContext = nullptr;
SwapBuffers = nullptr;
SwapIntervalEXT = nullptr;
SwapIntervalSGI = nullptr;
SwapIntervalMESA = nullptr;
QueryDrawable = nullptr;
MakeCurrent = nullptr;
2020-03-10 05:19:18 +00:00
}
2020-04-01 12:20:22 +00:00
glx_loader glx;