| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/gpu/ganesh/gl/glx/GrGLMakeGLXInterface.h" |
| 9 |
#include "include/private/base/SkOnce.h" |
| 10 |
#include "tools/ganesh/gl/GLTestContext.h" |
| 11 |
|
| 12 |
#include <X11/Xlib.h> |
| 13 |
#include <GL/glx.h> |
| 14 |
#include <GL/glu.h> |
| 15 |
|
| 16 |
#include <vector> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
/* Note: Skia requires glx 1.3 or newer */ |
| 22 |
|
| 23 |
/* This struct is taken from a mesa demo. Please update as required */ |
| 24 |
static const std::vector<std::pair<int, int>> gl_versions = { |
| 25 |
{1, 0}, |
| 26 |
{1, 1}, |
| 27 |
{1, 2}, |
| 28 |
{1, 3}, |
| 29 |
{1, 4}, |
| 30 |
{1, 5}, |
| 31 |
{2, 0}, |
| 32 |
{2, 1}, |
| 33 |
{3, 0}, |
| 34 |
{3, 1}, |
| 35 |
{3, 2}, |
| 36 |
{3, 3}, |
| 37 |
{4, 0}, |
| 38 |
{4, 1}, |
| 39 |
{4, 2}, |
| 40 |
{4, 3}, |
| 41 |
{4, 4}, |
| 42 |
}; |
| 43 |
|
| 44 |
static const std::vector<std::pair<int, int>> gles_versions = { |
| 45 |
{2, 0}, |
| 46 |
{3, 0}, |
| 47 |
}; |
| 48 |
|
| 49 |
static bool ctxErrorOccurred = false; |
| 50 |
static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) { |
| 51 |
ctxErrorOccurred = true; |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
class GLXGLTestContext : public sk_gpu_test::GLTestContext { |
| 56 |
public: |
| 57 |
GLXGLTestContext(GrGLStandard forcedGpuAPI, GLXGLTestContext* shareList); |
| 58 |
~GLXGLTestContext() override; |
| 59 |
|
| 60 |
private: |
| 61 |
void destroyGLContext(); |
| 62 |
static GLXContext CreateBestContext(bool isES, Display* display, GLXFBConfig bestFbc, |
| 63 |
GLXContext glxSharedContext); |
| 64 |
|
| 65 |
void onPlatformMakeNotCurrent() const override; |
| 66 |
void onPlatformMakeCurrent() const override; |
| 67 |
std::function<void()> onPlatformGetAutoContextRestore() const override; |
| 68 |
GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; |
| 69 |
|
| 70 |
GLXContext fContext; |
| 71 |
Display* fDisplay; |
| 72 |
Pixmap fPixmap; |
| 73 |
GLXPixmap fGlxPixmap; |
| 74 |
}; |
| 75 |
|
| 76 |
static Display* get_display() { |
| 77 |
class AutoDisplay { |
| 78 |
public: |
| 79 |
AutoDisplay() { fDisplay = XOpenDisplay(nullptr); } |
| 80 |
~AutoDisplay() { |
| 81 |
if (fDisplay) { |
| 82 |
XCloseDisplay(fDisplay); |
| 83 |
} |
| 84 |
} |
| 85 |
Display* display() const { return fDisplay; } |
| 86 |
private: |
| 87 |
Display* fDisplay; |
| 88 |
}; |
| 89 |
static std::unique_ptr<AutoDisplay> ad = std::make_unique<AutoDisplay>(); |
| 90 |
return ad->display(); |
| 91 |
} |
| 92 |
|
| 93 |
std::function<void()> context_restorer() { |
| 94 |
auto display = glXGetCurrentDisplay(); |
| 95 |
auto drawable = glXGetCurrentDrawable(); |
| 96 |
auto context = glXGetCurrentContext(); |
| 97 |
// On some systems calling glXMakeCurrent with a null display crashes. |
| 98 |
if (!display) { |
| 99 |
display = get_display(); |
| 100 |
} |
| 101 |
return [display, drawable, context] { glXMakeCurrent(display, drawable, context); }; |
| 102 |
} |
| 103 |
|
| 104 |
GLXGLTestContext::GLXGLTestContext(GrGLStandard forcedGpuAPI, GLXGLTestContext* shareContext) |
| 105 |
: fContext(nullptr) |
| 106 |
, fDisplay(nullptr) |
| 107 |
, fPixmap(0) |
| 108 |
, fGlxPixmap(0) { |
| 109 |
// We cross our fingers that this is the first X call in the program and that if the application |
| 110 |
// is actually threaded that this succeeds. |
| 111 |
static SkOnce gOnce; |
| 112 |
gOnce([] { XInitThreads(); }); |
| 113 |
|
| 114 |
fDisplay = get_display(); |
| 115 |
|
| 116 |
GLXContext glxShareContext = shareContext ? shareContext->fContext : nullptr; |
| 117 |
|
| 118 |
if (!fDisplay) { |
| 119 |
SkDebugf("Failed to open X display.\n"); |
| 120 |
this->destroyGLContext(); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// Get a matching FB config |
| 125 |
static int visual_attribs[] = { |
| 126 |
GLX_X_RENDERABLE , True, |
| 127 |
GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT, |
| 128 |
None |
| 129 |
}; |
| 130 |
|
| 131 |
int glx_major, glx_minor; |
| 132 |
|
| 133 |
// FBConfigs were added in GLX version 1.3. |
| 134 |
if (!glXQueryVersion(fDisplay, &glx_major, &glx_minor) || |
| 135 |
((glx_major == 1) && (glx_minor < 3)) || (glx_major < 1)) { |
| 136 |
SkDebugf("GLX version 1.3 or higher required.\n"); |
| 137 |
this->destroyGLContext(); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
//SkDebugf("Getting matching framebuffer configs.\n"); |
| 142 |
int fbcount; |
| 143 |
GLXFBConfig *fbc = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay), |
| 144 |
visual_attribs, &fbcount); |
| 145 |
if (!fbc) { |
| 146 |
SkDebugf("Failed to retrieve a framebuffer config.\n"); |
| 147 |
this->destroyGLContext(); |
| 148 |
return; |
| 149 |
} |
| 150 |
//SkDebugf("Found %d matching FB configs.\n", fbcount); |
| 151 |
|
| 152 |
// Pick the FB config/visual with the most samples per pixel |
| 153 |
//SkDebugf("Getting XVisualInfos.\n"); |
| 154 |
int best_fbc = -1, best_num_samp = -1; |
| 155 |
|
| 156 |
int i; |
| 157 |
for (i = 0; i < fbcount; ++i) { |
| 158 |
XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, fbc[i]); |
| 159 |
if (vi) { |
| 160 |
int samp_buf, samples; |
| 161 |
glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf); |
| 162 |
glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLES, &samples); |
| 163 |
|
| 164 |
//SkDebugf(" Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d," |
| 165 |
// " SAMPLES = %d\n", |
| 166 |
// i, (unsigned int)vi->visualid, samp_buf, samples); |
| 167 |
|
| 168 |
if (best_fbc < 0 || (samp_buf && samples > best_num_samp)) { |
| 169 |
best_fbc = i; |
| 170 |
best_num_samp = samples; |
| 171 |
} |
| 172 |
} |
| 173 |
XFree(vi); |
| 174 |
} |
| 175 |
|
| 176 |
GLXFBConfig bestFbc = fbc[best_fbc]; |
| 177 |
|
| 178 |
// Be sure to free the FBConfig list allocated by glXChooseFBConfig() |
| 179 |
XFree(fbc); |
| 180 |
|
| 181 |
// Get a visual |
| 182 |
XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, bestFbc); |
| 183 |
//SkDebugf("Chosen visual ID = 0x%x\n", (unsigned int)vi->visualid); |
| 184 |
|
| 185 |
fPixmap = XCreatePixmap(fDisplay, RootWindow(fDisplay, vi->screen), 10, 10, vi->depth); |
| 186 |
|
| 187 |
if (!fPixmap) { |
| 188 |
SkDebugf("Failed to create pixmap.\n"); |
| 189 |
this->destroyGLContext(); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); |
| 194 |
|
| 195 |
// Done with the visual info data |
| 196 |
XFree(vi); |
| 197 |
|
| 198 |
// Get the default screen's GLX extension list |
| 199 |
const char *glxExts = glXQueryExtensionsString( |
| 200 |
fDisplay, DefaultScreen(fDisplay) |
| 201 |
); |
| 202 |
// Check for the GLX_ARB_create_context extension string and the function. |
| 203 |
// If either is not present, use GLX 1.3 context creation method. |
| 204 |
if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_context"), |
| 205 |
reinterpret_cast<const GLubyte*>(glxExts))) { |
| 206 |
if (kGLES_GrGLStandard != forcedGpuAPI) { |
| 207 |
fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, nullptr, True); |
| 208 |
} |
| 209 |
} else { |
| 210 |
if (kGLES_GrGLStandard == forcedGpuAPI) { |
| 211 |
if (gluCheckExtension( |
| 212 |
reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2_profile"), |
| 213 |
reinterpret_cast<const GLubyte*>(glxExts))) { |
| 214 |
fContext = CreateBestContext(true, fDisplay, bestFbc, glxShareContext); |
| 215 |
} |
| 216 |
} else { |
| 217 |
fContext = CreateBestContext(false, fDisplay, bestFbc, glxShareContext); |
| 218 |
} |
| 219 |
} |
| 220 |
if (!fContext) { |
| 221 |
SkDebugf("Failed to create an OpenGL context.\n"); |
| 222 |
this->destroyGLContext(); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
// Verify that context is a direct context |
| 227 |
if (!glXIsDirect(fDisplay, fContext)) { |
| 228 |
//SkDebugf("Indirect GLX rendering context obtained.\n"); |
| 229 |
} else { |
| 230 |
//SkDebugf("Direct GLX rendering context obtained.\n"); |
| 231 |
} |
| 232 |
|
| 233 |
SkScopeExit restorer(context_restorer()); |
| 234 |
//SkDebugf("Making context current.\n"); |
| 235 |
if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { |
| 236 |
SkDebugf("Could not set the context.\n"); |
| 237 |
this->destroyGLContext(); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
#if defined(SK_GL) |
| 242 |
auto gl = GrGLInterfaces::MakeGLX(); |
| 243 |
if (!gl) { |
| 244 |
SkDebugf("Failed to create gl interface"); |
| 245 |
this->destroyGLContext(); |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
if (!gl->validate()) { |
| 250 |
SkDebugf("Failed to validate gl interface"); |
| 251 |
this->destroyGLContext(); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
this->init(std::move(gl)); |
| 256 |
#else |
| 257 |
// Allow the GLTestContext creation to succeed without a GrGLInterface to support |
| 258 |
// GrContextFactory's persistent GL context workaround for Vulkan. We won't need the |
| 259 |
// GrGLInterface since we're not running the GL backend. |
| 260 |
this->init(nullptr); |
| 261 |
#endif |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
GLXGLTestContext::~GLXGLTestContext() { |
| 266 |
this->teardown(); |
| 267 |
this->destroyGLContext(); |
| 268 |
} |
| 269 |
|
| 270 |
void GLXGLTestContext::destroyGLContext() { |
| 271 |
if (fDisplay) { |
| 272 |
if (fContext) { |
| 273 |
if (glXGetCurrentContext() == fContext) { |
| 274 |
// This will ensure that the context is immediately deleted. |
| 275 |
glXMakeContextCurrent(fDisplay, None, None, nullptr); |
| 276 |
} |
| 277 |
glXDestroyContext(fDisplay, fContext); |
| 278 |
fContext = nullptr; |
| 279 |
} |
| 280 |
|
| 281 |
if (fGlxPixmap) { |
| 282 |
glXDestroyGLXPixmap(fDisplay, fGlxPixmap); |
| 283 |
fGlxPixmap = 0; |
| 284 |
} |
| 285 |
|
| 286 |
if (fPixmap) { |
| 287 |
XFreePixmap(fDisplay, fPixmap); |
| 288 |
fPixmap = 0; |
| 289 |
} |
| 290 |
|
| 291 |
fDisplay = nullptr; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/* Create a context with the highest possible version. |
| 296 |
* |
| 297 |
* Disable Xlib errors for the duration of this function (by default they abort |
| 298 |
* the program) and try to get a context starting from the highest version |
| 299 |
* number - there is no way to just directly ask what the highest supported |
| 300 |
* version is. |
| 301 |
* |
| 302 |
* Returns the correct context or NULL on failure. |
| 303 |
*/ |
| 304 |
GLXContext GLXGLTestContext::CreateBestContext(bool isES, Display* display, GLXFBConfig bestFbc, |
| 305 |
GLXContext glxShareContext) { |
| 306 |
auto glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) |
| 307 |
glXGetProcAddressARB((const GrGLubyte*)"glXCreateContextAttribsARB"); |
| 308 |
if (!glXCreateContextAttribsARB) { |
| 309 |
SkDebugf("Failed to get address of glXCreateContextAttribsARB"); |
| 310 |
return nullptr; |
| 311 |
} |
| 312 |
GLXContext context = nullptr; |
| 313 |
// Install Xlib error handler that will set ctxErrorOccurred. |
| 314 |
// WARNING: It is global for all threads. |
| 315 |
ctxErrorOccurred = false; |
| 316 |
int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler); |
| 317 |
|
| 318 |
auto versions = isES ? gles_versions : gl_versions; |
| 319 |
// Well, unfortunately GLX will not just give us the highest context so |
| 320 |
// instead we have to do this nastiness |
| 321 |
for (int i = versions.size() - 1; i >= 0 ; i--) { |
| 322 |
// WARNING: Don't try to optimize this and make this array static. The |
| 323 |
// glXCreateContextAttribsARB call writes to it upon failure and the |
| 324 |
// next call would fail too. |
| 325 |
std::vector<int> flags = { |
| 326 |
GLX_CONTEXT_MAJOR_VERSION_ARB, versions[i].first, |
| 327 |
GLX_CONTEXT_MINOR_VERSION_ARB, versions[i].second, |
| 328 |
}; |
| 329 |
if (isES) { |
| 330 |
flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); |
| 331 |
// the ES2 flag should work even for higher versions |
| 332 |
flags.push_back(GLX_CONTEXT_ES2_PROFILE_BIT_EXT); |
| 333 |
} else if (versions[i].first > 2) { |
| 334 |
flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); |
| 335 |
flags.push_back(GLX_CONTEXT_CORE_PROFILE_BIT_ARB); |
| 336 |
} |
| 337 |
flags.push_back(0); |
| 338 |
context = glXCreateContextAttribsARB(display, bestFbc, glxShareContext, true, |
| 339 |
&flags[0]); |
| 340 |
// Sync to ensure any errors generated are processed. |
| 341 |
XSync(display, False); |
| 342 |
|
| 343 |
if (!ctxErrorOccurred && context) { |
| 344 |
break; |
| 345 |
} |
| 346 |
// try again |
| 347 |
ctxErrorOccurred = false; |
| 348 |
} |
| 349 |
// Restore the original error handler. |
| 350 |
XSetErrorHandler(oldHandler); |
| 351 |
return context; |
| 352 |
} |
| 353 |
|
| 354 |
void GLXGLTestContext::onPlatformMakeNotCurrent() const { |
| 355 |
if (!glXMakeCurrent(fDisplay, None , nullptr)) { |
| 356 |
SkDebugf("Could not reset the context.\n"); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
void GLXGLTestContext::onPlatformMakeCurrent() const { |
| 361 |
if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { |
| 362 |
SkDebugf("Could not set the context.\n"); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
std::function<void()> GLXGLTestContext::onPlatformGetAutoContextRestore() const { |
| 367 |
if (glXGetCurrentContext() == fContext) { |
| 368 |
return nullptr; |
| 369 |
} |
| 370 |
return context_restorer(); |
| 371 |
} |
| 372 |
|
| 373 |
GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) const { |
| 374 |
return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName)); |
| 375 |
} |
| 376 |
|
| 377 |
} // anonymous namespace |
| 378 |
|
| 379 |
namespace sk_gpu_test { |
| 380 |
GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, |
| 381 |
GLTestContext *shareContext) { |
| 382 |
GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(shareContext); |
| 383 |
GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext); |
| 384 |
if (!ctx->isValid()) { |
| 385 |
delete ctx; |
| 386 |
return nullptr; |
| 387 |
} |
| 388 |
return ctx; |
| 389 |
} |
| 390 |
} // namespace sk_gpu_test |