Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deviceinfo: Do not explode if there's no GL driver available. #1679

Merged
merged 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/os/device/deviceinfo/cc/android/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ bool createContext(void* platform_data) {
return true;
}

const char* contextError() {
return gContext.mError;
}
const char* contextError() { return gContext.mError; }

bool hasGLorGLES() { return true; }

int numABIs() { return gContext.mSupportedABIs.size(); }

Expand Down
4 changes: 0 additions & 4 deletions core/os/device/deviceinfo/cc/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ const char* safe_string(void* x) {
return (x != nullptr) ? reinterpret_cast<const char*>(x) : "";
}

bool hasGLorGLES() {
return core::hasGLorGLES();
}

void glDriver(device::OpenGLDriver* driver) {
GLint major_version = 2;
GLint minor_version = 0;
Expand Down
117 changes: 54 additions & 63 deletions core/os/device/deviceinfo/cc/linux/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Context {
char mError[512];
Display* mDisplay;
GLXFBConfig* mFBConfigs;
GLXContext mContext;
GLXContext mGlCtx;
GLXPbuffer mPbuffer;
int mNumCores;
utsname mUbuf;
Expand All @@ -57,7 +57,9 @@ typedef GLXContext (*pfn_glXCreateNewContext)(Display* dpy, GLXFBConfig config,
typedef GLXPbuffer (*pfn_glXCreatePbuffer)(Display* dpy, GLXFBConfig config, const int* attrib_list);
typedef void (*pfn_glXDestroyPbuffer)(Display* dpy, GLXPbuffer pbuf);
typedef void (*pfn_glXDestroyContext)(Display * dpy, GLXContext ctx);

typedef Bool (*pfn_glXMakeContextCurrent)(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx);
typedef GLXContext (*pfn_glXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context,
Bool direct, const int *attrib_list);

typedef int(*pfn_XFree) (void*);
typedef int(*pfn_XCloseDisplay)(Display*);
Expand All @@ -80,14 +82,13 @@ void destroyContext() {
pfn_glXDestroyPbuffer fn_glXDestroyPbuffer = (pfn_glXDestroyPbuffer)core::GetGlesProcAddress("glXDestroyPbuffer", true);
pfn_glXDestroyContext fn_glXDestroyContext = (pfn_glXDestroyContext)core::GetGlesProcAddress("glXDestroyContext", true);


if (gContext.mPbuffer && fn_glXDestroyPbuffer) {
(*fn_glXDestroyPbuffer)(gContext.mDisplay, gContext.mPbuffer);
gContext.mPbuffer = 0;
}
if (gContext.mContext && fn_glXDestroyContext) {
(*fn_glXDestroyContext)(gContext.mDisplay, gContext.mContext);
gContext.mContext = nullptr;
if (gContext.mGlCtx && fn_glXDestroyContext) {
(*fn_glXDestroyContext)(gContext.mDisplay, gContext.mGlCtx);
gContext.mGlCtx = nullptr;
}
if (gContext.mFBConfigs) {
fn_XFree(gContext.mFBConfigs);
Expand All @@ -99,38 +100,19 @@ void destroyContext() {
}
}

bool createContext(void* platform_data) {
if (gContextRefCount++ > 0) {
return true;
}

memset(&gContext, 0, sizeof(gContext));

if (uname(&gContext.mUbuf) != 0) {
snprintf(gContext.mError, sizeof(gContext.mError),
"uname returned error: %d", errno);
destroyContext();
return false;
}

gContext.mNumCores = sysconf(_SC_NPROCESSORS_CONF);

if (gethostname(gContext.mHostName, sizeof(gContext.mHostName)) != 0) {
snprintf(gContext.mError, sizeof(gContext.mError),
"gethostname returned error: %d", errno);
destroyContext();
return false;
}
void createGlContext() {
auto fn_glXChooseFBConfig= (pfn_glXChooseFBConfig)core::GetGlesProcAddress("glXChooseFBConfig", true);
auto fn_glXCreateNewContext = (pfn_glXCreateNewContext)core::GetGlesProcAddress("glXCreateNewContext", true);
auto fn_glXCreatePbuffer = (pfn_glXCreatePbuffer)core::GetGlesProcAddress("glXCreatePbuffer", true);
auto fn_glXMakeContextCurrent = (pfn_glXMakeContextCurrent)core::GetGlesProcAddress("glXMakeContextCurrent", true);
auto fn_glXCreateContextAttribsARB = (pfn_glXCreateContextAttribsARB)core::GetGlesProcAddress("glXCreateContextAttribsARB", true);

pfn_glXChooseFBConfig fn_glXChooseFBConfig= (pfn_glXChooseFBConfig)core::GetGlesProcAddress("glXChooseFBConfig", true);
pfn_glXCreateNewContext fn_glXCreateNewContext = (pfn_glXCreateNewContext)core::GetGlesProcAddress("glXCreateNewContext", true);
pfn_glXCreatePbuffer fn_glXCreatePbuffer = (pfn_glXCreatePbuffer)core::GetGlesProcAddress("glXCreatePbuffer", true);
if (!fn_glXChooseFBConfig || !fn_glXCreateNewContext || !fn_glXCreatePbuffer) {
return true;
if (!fn_glXChooseFBConfig || !fn_glXCreateNewContext || !fn_glXCreatePbuffer || !fn_glXMakeContextCurrent) {
return;
}

if (!core::DlLoader::can_load("libX11.so")) {
return true;
return;
}

core::DlLoader libX("libX11.so");
Expand All @@ -144,7 +126,7 @@ bool createContext(void* platform_data) {
// the bazel sandbox. Attempt to connect to the 0'th display instead.
gContext.mDisplay = fn_XOpenDisplay(":0");
if (gContext.mDisplay == nullptr) {
return true;
return;
}
}

Expand All @@ -165,22 +147,13 @@ bool createContext(void* platform_data) {
visualAttribs,
&fbConfigsCount);
if (!gContext.mFBConfigs) {
snprintf(gContext.mError, sizeof(gContext.mError),
"glXChooseFBConfig failed");
destroyContext();
return false;
return;
}

GLXFBConfig fbConfig = gContext.mFBConfigs[0];

typedef GLXContext (*pfn_glXCreateContextAttribsARB)(
Display *dpy, GLXFBConfig config, GLXContext share_context,
Bool direct, const int *attrib_list);

pfn_glXCreateContextAttribsARB fn_glXCreateContextAttribsARB =
(pfn_glXCreateContextAttribsARB)core::GetGlesProcAddress("glXCreateContextAttribsARB", true);
if (fn_glXCreateContextAttribsARB == nullptr) {
gContext.mContext = (*fn_glXCreateNewContext)(gContext.mDisplay,
gContext.mGlCtx = (*fn_glXCreateNewContext)(gContext.mDisplay,
fbConfig,
GLX_RGBA_TYPE,
nullptr,
Expand All @@ -198,44 +171,62 @@ bool createContext(void* platform_data) {
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None,
};
gContext.mContext = fn_glXCreateContextAttribsARB(
gContext.mGlCtx = fn_glXCreateContextAttribsARB(
gContext.mDisplay, fbConfig, nullptr, /* direct */ True, contextAttribs);
if (gContext.mContext != nullptr) {
if (gContext.mGlCtx != nullptr) {
break;
}
}
fn_XSetErrorHandler(oldHandler);
}

if (!gContext.mContext) {
snprintf(gContext.mError, sizeof(gContext.mError),
"glXCreateNewContext failed");
destroyContext();
return false;
if (!gContext.mGlCtx) {
return;
}

const int pbufferAttribs[] = {
GLX_PBUFFER_WIDTH, 32, GLX_PBUFFER_HEIGHT, 32, None
};

gContext.mPbuffer = (*fn_glXCreatePbuffer)(gContext.mDisplay, fbConfig, pbufferAttribs);
gContext.mPbuffer = fn_glXCreatePbuffer(gContext.mDisplay, fbConfig, pbufferAttribs);
if (!gContext.mPbuffer) {
return;
}

fn_glXMakeContextCurrent(gContext.mDisplay, gContext.mPbuffer, gContext.mPbuffer, gContext.mGlCtx);
}

bool createContext(void* platform_data) {
if (gContextRefCount++ > 0) {
return true;
}

memset(&gContext, 0, sizeof(gContext));

if (uname(&gContext.mUbuf) != 0) {
snprintf(gContext.mError, sizeof(gContext.mError),
"uname returned error: %d", errno);
destroyContext();
return false;
}

gContext.mNumCores = sysconf(_SC_NPROCESSORS_CONF);

if (gethostname(gContext.mHostName, sizeof(gContext.mHostName)) != 0) {
snprintf(gContext.mError, sizeof(gContext.mError),
"glXCreatePbuffer failed");
"gethostname returned error: %d", errno);
destroyContext();
return false;
}
typedef Bool
(*pfn_glXMakeContextCurrent)(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx);

pfn_glXMakeContextCurrent fn_glXMakeContextCurrent =
(pfn_glXMakeContextCurrent)core::GetGlesProcAddress("glXMakeContextCurrent", true);
fn_glXMakeContextCurrent(gContext.mDisplay, gContext.mPbuffer, gContext.mPbuffer, gContext.mContext);
createGlContext();

return true;
}

const char* contextError() {
return gContext.mError;
}
const char* contextError() { return gContext.mError; }

bool hasGLorGLES() { return gContext.mGlCtx != nullptr; }

int numABIs() { return 1; }

Expand Down
50 changes: 23 additions & 27 deletions core/os/device/deviceinfo/cc/osx/query.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ void destroyContext() {
}
}

bool createContext(void* platform_data) {
if (gContextRefCount++ > 0) {
return true;
}

memset(&gContext, 0, sizeof(gContext));

void createGlContext() {
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFANoRecovery,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)32,
Expand All @@ -79,6 +73,24 @@ bool createContext(void* platform_data) {
(NSOpenGLPixelFormatAttribute)0
};

gContext.mGlFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
if (gContext.mGlFmt != nullptr) {
gContext.mGlCtx = [[NSOpenGLContext alloc] initWithFormat:gContext.mGlFmt shareContext:nil];
if (gContext.mGlCtx == nullptr) {
return;
}

[gContext.mGlCtx makeCurrentContext];
}
}

bool createContext(void* platform_data) {
if (gContextRefCount++ > 0) {
return true;
}

memset(&gContext, 0, sizeof(gContext));

size_t len = 0;
int mib[2] = {CTL_HW, HW_MODEL};
sysctl(mib, 2, nullptr, &len, nullptr, 0);
Expand All @@ -105,32 +117,16 @@ bool createContext(void* platform_data) {
return false;
}

gContext.mGlFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
if (gContext.mGlFmt == 0) {
snprintf(gContext.mError, sizeof(gContext.mError),
"NSOpenGLPixelFormat alloc failed");
destroyContext();
return false;
}

gContext.mGlCtx = [[NSOpenGLContext alloc] initWithFormat:gContext.mGlFmt shareContext:nil];
if (gContext.mGlCtx == 0) {
snprintf(gContext.mError, sizeof(gContext.mError),
"NSOpenGLContext alloc failed");
destroyContext();
return false;
}

[gContext.mGlCtx makeCurrentContext];
createGlContext();

gContext.mOsVersion = [[NSProcessInfo processInfo] operatingSystemVersion];

return true;
}

const char* contextError() {
return gContext.mError;
}
const char* contextError() { return gContext.mError; }

bool hasGLorGLES() { return gContext.mGlCtx != nullptr; }

int numABIs() { return 1; }

Expand Down
3 changes: 2 additions & 1 deletion core/os/device/deviceinfo/cc/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ bool vkPhysicalDevices(
// false.
bool hasVulkanLoader();

// hasGLorGLES returns true if libGL or libGLES can be found.
// hasGLorGLES returns true if there is a OpenGL or OpenGL ES display driver
// that can be used.
bool hasGLorGLES();

// The functions below are used by getDeviceInstance(), and are implemented
Expand Down
Loading