2016-05-31 16:39:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/gl/GrGLInterface.h"
|
|
|
|
#include "tools/sk_app/GLWindowContext.h"
|
|
|
|
#include "tools/sk_app/unix/WindowContextFactory_unix.h"
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2018-09-19 15:31:27 +00:00
|
|
|
#include <GL/gl.h>
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
using sk_app::window_context_factory::XlibWindowInfo;
|
|
|
|
using sk_app::DisplayParams;
|
|
|
|
using sk_app::GLWindowContext;
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
namespace {
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2018-09-05 16:40:53 +00:00
|
|
|
static bool gCtxErrorOccurred = false;
|
|
|
|
static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
|
|
|
|
gCtxErrorOccurred = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
class GLWindowContext_xlib : public GLWindowContext {
|
|
|
|
public:
|
|
|
|
GLWindowContext_xlib(const XlibWindowInfo&, const DisplayParams&);
|
|
|
|
~GLWindowContext_xlib() override;
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
void onSwapBuffers() override;
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
void onDestroyContext() override;
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
protected:
|
2017-08-17 18:37:06 +00:00
|
|
|
sk_sp<const GrGLInterface> onInitializeContext() override;
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
private:
|
|
|
|
GLWindowContext_xlib(void*, const DisplayParams&);
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
Display* fDisplay;
|
|
|
|
XWindow fWindow;
|
2017-03-08 15:50:21 +00:00
|
|
|
GLXFBConfig* fFBConfig;
|
2016-07-26 19:02:50 +00:00
|
|
|
XVisualInfo* fVisualInfo;
|
|
|
|
GLXContext fGLContext;
|
2017-05-02 20:15:53 +00:00
|
|
|
|
|
|
|
typedef GLWindowContext INHERITED;
|
2016-07-26 19:02:50 +00:00
|
|
|
};
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
GLWindowContext_xlib::GLWindowContext_xlib(const XlibWindowInfo& winInfo, const DisplayParams& params)
|
2017-05-02 20:15:53 +00:00
|
|
|
: INHERITED(params)
|
2016-07-26 19:02:50 +00:00
|
|
|
, fDisplay(winInfo.fDisplay)
|
|
|
|
, fWindow(winInfo.fWindow)
|
2017-03-08 15:50:21 +00:00
|
|
|
, fFBConfig(winInfo.fFBConfig)
|
2016-07-26 19:02:50 +00:00
|
|
|
, fVisualInfo(winInfo.fVisualInfo)
|
|
|
|
, fGLContext() {
|
2017-02-24 20:22:53 +00:00
|
|
|
fWidth = winInfo.fWidth;
|
|
|
|
fHeight = winInfo.fHeight;
|
2016-07-26 19:02:50 +00:00
|
|
|
this->initializeContext();
|
|
|
|
}
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2017-03-08 15:50:21 +00:00
|
|
|
using CreateContextAttribsFn = GLXContext(Display*, GLXFBConfig, GLXContext, Bool, const int*);
|
|
|
|
|
2017-08-17 18:37:06 +00:00
|
|
|
sk_sp<const GrGLInterface> GLWindowContext_xlib::onInitializeContext() {
|
2016-07-26 19:02:50 +00:00
|
|
|
SkASSERT(fDisplay);
|
2017-03-08 15:50:21 +00:00
|
|
|
SkASSERT(!fGLContext);
|
2018-05-04 15:44:40 +00:00
|
|
|
sk_sp<const GrGLInterface> interface;
|
|
|
|
bool current = false;
|
2019-03-25 17:42:33 +00:00
|
|
|
|
2017-03-08 15:50:21 +00:00
|
|
|
// We attempt to use glXCreateContextAttribsARB as RenderDoc requires that the context be
|
|
|
|
// created with this rather than glXCreateContext.
|
|
|
|
CreateContextAttribsFn* createContextAttribs = (CreateContextAttribsFn*)glXGetProcAddressARB(
|
|
|
|
(const GLubyte*)"glXCreateContextAttribsARB");
|
|
|
|
if (createContextAttribs && fFBConfig) {
|
2018-09-05 16:40:53 +00:00
|
|
|
// Install Xlib error handler that will set gCtxErrorOccurred
|
|
|
|
int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
|
|
|
|
|
2017-03-08 15:50:21 +00:00
|
|
|
// Specifying 3.2 allows an arbitrarily high context version (so long as no 3.2 features
|
|
|
|
// have been removed).
|
|
|
|
for (int minor = 2; minor >= 0 && !fGLContext; --minor) {
|
|
|
|
// Ganesh prefers a compatibility profile for possible NVPR support. However, RenderDoc
|
|
|
|
// requires a core profile. Edit this code to use RenderDoc.
|
|
|
|
for (int profile : {GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
|
|
|
|
GLX_CONTEXT_CORE_PROFILE_BIT_ARB}) {
|
2018-09-05 16:40:53 +00:00
|
|
|
gCtxErrorOccurred = false;
|
2017-03-08 15:50:21 +00:00
|
|
|
int attribs[] = {
|
|
|
|
GLX_CONTEXT_MAJOR_VERSION_ARB, 3, GLX_CONTEXT_MINOR_VERSION_ARB, minor,
|
|
|
|
GLX_CONTEXT_PROFILE_MASK_ARB, profile,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
fGLContext = createContextAttribs(fDisplay, *fFBConfig, nullptr, True, attribs);
|
2018-09-05 16:40:53 +00:00
|
|
|
|
|
|
|
// Sync to ensure any errors generated are processed.
|
|
|
|
XSync(fDisplay, False);
|
|
|
|
if (gCtxErrorOccurred) { continue; }
|
|
|
|
|
2018-05-04 15:44:40 +00:00
|
|
|
if (fGLContext && profile == GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB &&
|
|
|
|
glXMakeCurrent(fDisplay, fWindow, fGLContext)) {
|
|
|
|
current = true;
|
|
|
|
// Look to see if RenderDoc is attached. If so, re-create the context with a
|
|
|
|
// core profile.
|
|
|
|
interface = GrGLMakeNativeInterface();
|
|
|
|
if (interface && interface->fExtensions.has("GL_EXT_debug_tool")) {
|
|
|
|
interface.reset();
|
|
|
|
glXMakeCurrent(fDisplay, None, nullptr);
|
|
|
|
glXDestroyContext(fDisplay, fGLContext);
|
|
|
|
current = false;
|
|
|
|
fGLContext = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 15:50:21 +00:00
|
|
|
if (fGLContext) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 16:40:53 +00:00
|
|
|
// Restore the original error handler
|
|
|
|
XSetErrorHandler(oldHandler);
|
2017-03-08 15:50:21 +00:00
|
|
|
}
|
|
|
|
if (!fGLContext) {
|
|
|
|
fGLContext = glXCreateContext(fDisplay, fVisualInfo, nullptr, GL_TRUE);
|
|
|
|
}
|
2016-05-31 16:39:20 +00:00
|
|
|
if (!fGLContext) {
|
2017-08-17 18:37:06 +00:00
|
|
|
return nullptr;
|
2016-05-31 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 15:44:40 +00:00
|
|
|
if (!current && !glXMakeCurrent(fDisplay, fWindow, fGLContext)) {
|
2017-08-17 18:37:06 +00:00
|
|
|
return nullptr;
|
2016-05-31 16:39:20 +00:00
|
|
|
}
|
2019-03-25 17:42:33 +00:00
|
|
|
|
|
|
|
const char* glxExtensions = glXQueryExtensionsString(fDisplay, DefaultScreen(fDisplay));
|
|
|
|
if (glxExtensions) {
|
|
|
|
if (strstr(glxExtensions, "GLX_EXT_swap_control")) {
|
|
|
|
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT =
|
|
|
|
(PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB(
|
|
|
|
(const GLubyte*)"glXSwapIntervalEXT");
|
|
|
|
glXSwapIntervalEXT(fDisplay, fWindow, fDisplayParams.fDisableVsync ? 0 : 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:37:06 +00:00
|
|
|
glClearStencil(0);
|
|
|
|
glClearColor(0, 0, 0, 0);
|
|
|
|
glStencilMask(0xffffffff);
|
|
|
|
glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
glXGetConfig(fDisplay, fVisualInfo, GLX_STENCIL_SIZE, &fStencilBits);
|
|
|
|
glXGetConfig(fDisplay, fVisualInfo, GLX_SAMPLES_ARB, &fSampleCount);
|
2020-02-07 15:36:46 +00:00
|
|
|
fSampleCount = std::max(fSampleCount, 1);
|
2017-08-17 18:37:06 +00:00
|
|
|
|
|
|
|
XWindow root;
|
|
|
|
int x, y;
|
|
|
|
unsigned int border_width, depth;
|
|
|
|
XGetGeometry(fDisplay, fWindow, &root, &x, &y, (unsigned int*)&fWidth, (unsigned int*)&fHeight,
|
|
|
|
&border_width, &depth);
|
|
|
|
glViewport(0, 0, fWidth, fHeight);
|
|
|
|
|
2018-05-04 15:44:40 +00:00
|
|
|
return interface ? interface : GrGLMakeNativeInterface();
|
2016-05-31 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
GLWindowContext_xlib::~GLWindowContext_xlib() {
|
|
|
|
this->destroyContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext_xlib::onDestroyContext() {
|
2016-05-31 16:39:20 +00:00
|
|
|
if (!fDisplay || !fGLContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
glXMakeCurrent(fDisplay, None, nullptr);
|
|
|
|
glXDestroyContext(fDisplay, fGLContext);
|
|
|
|
fGLContext = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
void GLWindowContext_xlib::onSwapBuffers() {
|
2016-05-31 16:39:20 +00:00
|
|
|
if (fDisplay && fGLContext) {
|
|
|
|
glXSwapBuffers(fDisplay, fWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
|
|
|
|
namespace window_context_factory {
|
|
|
|
|
2019-08-01 20:21:49 +00:00
|
|
|
std::unique_ptr<WindowContext> MakeGLForXlib(const XlibWindowInfo& winInfo,
|
|
|
|
const DisplayParams& params) {
|
|
|
|
std::unique_ptr<WindowContext> ctx(new GLWindowContext_xlib(winInfo, params));
|
2016-07-26 19:02:50 +00:00
|
|
|
if (!ctx->isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace window_context_factory
|
2016-05-31 16:39:20 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
} // namespace sk_app
|