24static const std::vector<std::pair<int, int>> gl_versions = {
44static const std::vector<std::pair<int, int>> gles_versions = {
49static bool ctxErrorOccurred =
false;
50static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
51 ctxErrorOccurred =
true;
57 GLXGLTestContext(
GrGLStandard forcedGpuAPI, GLXGLTestContext* shareList);
58 ~GLXGLTestContext()
override;
61 void destroyGLContext();
62 static GLXContext CreateBestContext(
bool isES, Display* display, GLXFBConfig bestFbc,
63 GLXContext glxSharedContext);
65 void onPlatformMakeNotCurrent()
const override;
66 void onPlatformMakeCurrent()
const override;
67 std::function<void()> onPlatformGetAutoContextRestore()
const override;
68 GrGLFuncPtr onPlatformGetProcAddress(
const char*)
const override;
76static Display* get_display() {
79 AutoDisplay() { fDisplay = XOpenDisplay(
nullptr); }
82 XCloseDisplay(fDisplay);
85 Display* display()
const {
return fDisplay; }
89 static std::unique_ptr<AutoDisplay> ad;
91 once([] { ad = std::make_unique<AutoDisplay>(); });
96 auto display = glXGetCurrentDisplay();
97 auto drawable = glXGetCurrentDrawable();
98 auto context = glXGetCurrentContext();
101 display = get_display();
103 return [display, drawable, context] { glXMakeCurrent(display, drawable, context); };
106GLXGLTestContext::GLXGLTestContext(
GrGLStandard forcedGpuAPI, GLXGLTestContext* shareContext)
114 gOnce([] { XInitThreads(); });
116 fDisplay = get_display();
118 GLXContext glxShareContext = shareContext ? shareContext->fContext :
nullptr;
121 SkDebugf(
"Failed to open X display.\n");
122 this->destroyGLContext();
127 static int visual_attribs[] = {
128 GLX_X_RENDERABLE , True,
129 GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT,
133 int glx_major, glx_minor;
136 if (!glXQueryVersion(fDisplay, &glx_major, &glx_minor) ||
137 ((glx_major == 1) && (glx_minor < 3)) || (glx_major < 1)) {
138 SkDebugf(
"GLX version 1.3 or higher required.\n");
139 this->destroyGLContext();
145 GLXFBConfig *fbc = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay),
146 visual_attribs, &fbcount);
148 SkDebugf(
"Failed to retrieve a framebuffer config.\n");
149 this->destroyGLContext();
156 int best_fbc = -1, best_num_samp = -1;
159 for (
i = 0;
i < fbcount; ++
i) {
160 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, fbc[
i]);
162 int samp_buf, samples;
163 glXGetFBConfigAttrib(fDisplay, fbc[
i], GLX_SAMPLE_BUFFERS, &samp_buf);
164 glXGetFBConfigAttrib(fDisplay, fbc[
i], GLX_SAMPLES, &samples);
170 if (best_fbc < 0 || (samp_buf && samples > best_num_samp)) {
172 best_num_samp = samples;
178 GLXFBConfig bestFbc = fbc[best_fbc];
184 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, bestFbc);
187 fPixmap = XCreatePixmap(fDisplay, RootWindow(fDisplay, vi->screen), 10, 10, vi->depth);
190 SkDebugf(
"Failed to create pixmap.\n");
191 this->destroyGLContext();
195 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap);
201 const char *glxExts = glXQueryExtensionsString(
202 fDisplay, DefaultScreen(fDisplay)
206 if (!gluCheckExtension(
reinterpret_cast<const GLubyte*
>(
"GLX_ARB_create_context"),
207 reinterpret_cast<const GLubyte*
>(glxExts))) {
209 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE,
nullptr, True);
213 if (gluCheckExtension(
214 reinterpret_cast<const GLubyte*
>(
"GLX_EXT_create_context_es2_profile"),
215 reinterpret_cast<const GLubyte*
>(glxExts))) {
216 fContext = CreateBestContext(
true, fDisplay, bestFbc, glxShareContext);
219 fContext = CreateBestContext(
false, fDisplay, bestFbc, glxShareContext);
223 SkDebugf(
"Failed to create an OpenGL context.\n");
224 this->destroyGLContext();
229 if (!glXIsDirect(fDisplay,
fContext)) {
237 if (!glXMakeCurrent(fDisplay, fGlxPixmap,
fContext)) {
238 SkDebugf(
"Could not set the context.\n");
239 this->destroyGLContext();
246 SkDebugf(
"Failed to create gl interface");
247 this->destroyGLContext();
251 if (!
gl->validate()) {
252 SkDebugf(
"Failed to validate gl interface");
253 this->destroyGLContext();
257 this->
init(std::move(
gl));
267GLXGLTestContext::~GLXGLTestContext() {
269 this->destroyGLContext();
272void GLXGLTestContext::destroyGLContext() {
275 if (glXGetCurrentContext() ==
fContext) {
277 glXMakeContextCurrent(fDisplay, None, None,
nullptr);
279 glXDestroyContext(fDisplay,
fContext);
284 glXDestroyGLXPixmap(fDisplay, fGlxPixmap);
289 XFreePixmap(fDisplay, fPixmap);
306GLXContext GLXGLTestContext::CreateBestContext(
bool isES, Display* display, GLXFBConfig bestFbc,
307 GLXContext glxShareContext) {
308 auto glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
309 glXGetProcAddressARB((
const GrGLubyte*)
"glXCreateContextAttribsARB");
310 if (!glXCreateContextAttribsARB) {
311 SkDebugf(
"Failed to get address of glXCreateContextAttribsARB");
314 GLXContext context =
nullptr;
317 ctxErrorOccurred =
false;
318 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
320 auto versions = isES ? gles_versions : gl_versions;
323 for (
int i = versions.size() - 1;
i >= 0 ;
i--) {
327 std::vector<int>
flags = {
328 GLX_CONTEXT_MAJOR_VERSION_ARB, versions[
i].first,
329 GLX_CONTEXT_MINOR_VERSION_ARB, versions[
i].second,
332 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB);
334 flags.push_back(GLX_CONTEXT_ES2_PROFILE_BIT_EXT);
335 }
else if (versions[
i].first > 2) {
336 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB);
337 flags.push_back(GLX_CONTEXT_CORE_PROFILE_BIT_ARB);
340 context = glXCreateContextAttribsARB(display, bestFbc, glxShareContext,
true,
343 XSync(display,
False);
345 if (!ctxErrorOccurred && context) {
349 ctxErrorOccurred =
false;
352 XSetErrorHandler(oldHandler);
356void GLXGLTestContext::onPlatformMakeNotCurrent()
const {
357 if (!glXMakeCurrent(fDisplay, None ,
nullptr)) {
358 SkDebugf(
"Could not reset the context.\n");
362void GLXGLTestContext::onPlatformMakeCurrent()
const {
363 if (!glXMakeCurrent(fDisplay, fGlxPixmap,
fContext)) {
364 SkDebugf(
"Could not set the context.\n");
368std::function<void()> GLXGLTestContext::onPlatformGetAutoContextRestore()
const {
369 if (glXGetCurrentContext() ==
fContext) {
372 return context_restorer();
375GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(
const char* procName)
const {
376 return glXGetProcAddress(
reinterpret_cast<const GLubyte*
>(procName));
383 GLTestContext *shareContext) {
384 GLXGLTestContext *glxShareContext =
reinterpret_cast<GLXGLTestContext *
>(shareContext);
385 GLXGLTestContext *ctx =
new GLXGLTestContext(forcedGpuAPI, glxShareContext);
386 if (!ctx->isValid()) {
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
FlutterSemanticsFlag flags
Dart_NativeFunction function
SK_API sk_sp< const GrGLInterface > MakeGLX()
GLTestContext * CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, GLTestContext *shareContext)