blob: b91262f4ef3246515be10a3fe6f208d586a17a25 [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
kkinnunen9e61bb72014-10-09 05:24:15 -07008#include "gl/SkGLContext.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +00009
kkinnunen9e61bb72014-10-09 05:24:15 -070010#include <X11/Xlib.h>
11#include <GL/glx.h>
bsalomon@google.com373a6632011-10-19 20:43:20 +000012#include <GL/glu.h>
13
kkinnunen9e61bb72014-10-09 05:24:15 -070014namespace {
15
joshualittc863ab02014-08-07 13:48:49 -070016/* Note: Skia requires glx 1.3 or newer */
robertphillips@google.combb89cda2012-03-14 18:06:26 +000017
kkinnunen9e61bb72014-10-09 05:24:15 -070018/* This struct is taken from a mesa demo. Please update as required */
19static const struct { int major, minor; } gl_versions[] = {
20 {1, 0},
21 {1, 1},
22 {1, 2},
23 {1, 3},
24 {1, 4},
25 {1, 5},
26 {2, 0},
27 {2, 1},
28 {3, 0},
29 {3, 1},
30 {3, 2},
31 {3, 3},
32 {4, 0},
33 {4, 1},
34 {4, 2},
35 {4, 3},
36 {4, 4},
37 {0, 0} /* end of list */
38};
39#define NUM_GL_VERSIONS SK_ARRAY_COUNT(gl_versions)
bsalomon@google.com373a6632011-10-19 20:43:20 +000040
41static bool ctxErrorOccurred = false;
42static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
43 ctxErrorOccurred = true;
44 return 0;
45}
46
kkinnunen9e61bb72014-10-09 05:24:15 -070047class GLXGLContext : public SkGLContext {
48public:
joshualittb59d1bc2016-01-20 08:07:01 -080049 GLXGLContext(GrGLStandard forcedGpuAPI, GLXGLContext* shareList);
mtklein36352bf2015-03-25 18:17:31 -070050 ~GLXGLContext() override;
kkinnunen9e61bb72014-10-09 05:24:15 -070051
52private:
kkinnunen30bc88c2014-10-15 23:03:54 -070053 void destroyGLContext();
54
cdaltond416a5b2015-06-23 13:23:44 -070055 void onPlatformMakeCurrent() const override;
56 void onPlatformSwapBuffers() const override;
57 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
58
kkinnunen9e61bb72014-10-09 05:24:15 -070059 GLXContext fContext;
60 Display* fDisplay;
61 Pixmap fPixmap;
62 GLXPixmap fGlxPixmap;
63};
64
joshualittb59d1bc2016-01-20 08:07:01 -080065GLXGLContext::GLXGLContext(GrGLStandard forcedGpuAPI, GLXGLContext* shareContext)
halcanary96fcdcc2015-08-27 07:41:13 -070066 : fContext(nullptr)
67 , fDisplay(nullptr)
bsalomon@google.com373a6632011-10-19 20:43:20 +000068 , fPixmap(0)
69 , fGlxPixmap(0) {
bsalomon@google.com373a6632011-10-19 20:43:20 +000070 fDisplay = XOpenDisplay(0);
71
joshualittb59d1bc2016-01-20 08:07:01 -080072 GLXContext glxShareContext = shareContext ? shareContext->fContext : nullptr;
73
bsalomon@google.com373a6632011-10-19 20:43:20 +000074 if (!fDisplay) {
75 SkDebugf("Failed to open X display.\n");
76 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -070077 return;
bsalomon@google.com373a6632011-10-19 20:43:20 +000078 }
79
80 // Get a matching FB config
81 static int visual_attribs[] = {
82 GLX_X_RENDERABLE , True,
83 GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT,
84 None
85 };
86
joshualittc863ab02014-08-07 13:48:49 -070087 int glx_major, glx_minor;
88
89 // FBConfigs were added in GLX version 1.3.
90 if (!glXQueryVersion(fDisplay, &glx_major, &glx_minor) ||
91 ((glx_major == 1) && (glx_minor < 3)) || (glx_major < 1)) {
92 SkDebugf("GLX version 1.3 or higher required.\n");
93 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -070094 return;
joshualittc863ab02014-08-07 13:48:49 -070095 }
96
bsalomon@google.com373a6632011-10-19 20:43:20 +000097 //SkDebugf("Getting matching framebuffer configs.\n");
98 int fbcount;
99 GLXFBConfig *fbc = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay),
100 visual_attribs, &fbcount);
101 if (!fbc) {
102 SkDebugf("Failed to retrieve a framebuffer config.\n");
103 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -0700104 return;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000105 }
106 //SkDebugf("Found %d matching FB configs.\n", fbcount);
107
108 // Pick the FB config/visual with the most samples per pixel
109 //SkDebugf("Getting XVisualInfos.\n");
robertphillips@google.combb89cda2012-03-14 18:06:26 +0000110 int best_fbc = -1, best_num_samp = -1;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000111
112 int i;
113 for (i = 0; i < fbcount; ++i) {
114 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, fbc[i]);
115 if (vi) {
116 int samp_buf, samples;
117 glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf);
118 glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLES, &samples);
119
120 //SkDebugf(" Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d,"
121 // " SAMPLES = %d\n",
122 // i, (unsigned int)vi->visualid, samp_buf, samples);
123
124 if (best_fbc < 0 || (samp_buf && samples > best_num_samp))
125 best_fbc = i, best_num_samp = samples;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000126 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000127 XFree(vi);
128 }
129
130 GLXFBConfig bestFbc = fbc[best_fbc];
131
132 // Be sure to free the FBConfig list allocated by glXChooseFBConfig()
133 XFree(fbc);
134
135 // Get a visual
136 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, bestFbc);
137 //SkDebugf("Chosen visual ID = 0x%x\n", (unsigned int)vi->visualid);
138
139 fPixmap = XCreatePixmap(fDisplay, RootWindow(fDisplay, vi->screen), 10, 10, vi->depth);
140
141 if (!fPixmap) {
142 SkDebugf("Failed to create pixmap.\n");
143 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -0700144 return;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000145 }
146
147 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap);
148
149 // Done with the visual info data
150 XFree(vi);
151
152 // Create the context
153
154 // Install an X error handler so the application won't exit if GL 3.0
155 // context allocation fails.
156 //
157 // Note this error handler is global.
158 // All display connections in all threads of a process use the same
159 // error handler, so be sure to guard against other threads issuing
160 // X commands while this code is running.
161 ctxErrorOccurred = false;
162 int (*oldHandler)(Display*, XErrorEvent*) =
163 XSetErrorHandler(&ctxErrorHandler);
164
165 // Get the default screen's GLX extension list
166 const char *glxExts = glXQueryExtensionsString(
167 fDisplay, DefaultScreen(fDisplay)
168 );
kkinnunen80549fc2014-06-30 06:36:31 -0700169
170
bsalomon@google.com373a6632011-10-19 20:43:20 +0000171 // Check for the GLX_ARB_create_context extension string and the function.
172 // If either is not present, use GLX 1.3 context creation method.
kkinnunen80549fc2014-06-30 06:36:31 -0700173 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_context"),
174 reinterpret_cast<const GLubyte*>(glxExts))) {
175 if (kGLES_GrGLStandard != forcedGpuAPI) {
kkinnunen80549fc2014-06-30 06:36:31 -0700176 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0, True);
kkinnunen80549fc2014-06-30 06:36:31 -0700177 }
joshualittc863ab02014-08-07 13:48:49 -0700178 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000179 //SkDebugf("Creating context.\n");
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000180 PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB =
bsalomon@google.com373a6632011-10-19 20:43:20 +0000181 (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((GrGLubyte*)"glXCreateContextAttribsARB");
kkinnunen80549fc2014-06-30 06:36:31 -0700182
kkinnunen80549fc2014-06-30 06:36:31 -0700183 if (kGLES_GrGLStandard == forcedGpuAPI) {
184 if (gluCheckExtension(
185 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2_profile"),
186 reinterpret_cast<const GLubyte*>(glxExts))) {
joshualittc863ab02014-08-07 13:48:49 -0700187 static const int context_attribs_gles[] = {
188 GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
189 GLX_CONTEXT_MINOR_VERSION_ARB, 0,
190 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
191 None
192 };
joshualittb59d1bc2016-01-20 08:07:01 -0800193 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, glxShareContext, True,
kkinnunen80549fc2014-06-30 06:36:31 -0700194 context_attribs_gles);
195 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000196 } else {
joshualittc863ab02014-08-07 13:48:49 -0700197 // Well, unfortunately GLX will not just give us the highest context so instead we have
198 // to do this nastiness
199 for (i = NUM_GL_VERSIONS - 2; i > 0 ; i--) {
200 /* don't bother below GL 3.0 */
201 if (gl_versions[i].major == 3 && gl_versions[i].minor == 0) {
202 break;
203 }
204 // On Nvidia GPUs, to use Nv Path rendering we need a compatibility profile for the
205 // time being.
206 // TODO when Nvidia implements NVPR on Core profiles, we should start requesting
207 // core here
208 static const int context_attribs_gl[] = {
209 GLX_CONTEXT_MAJOR_VERSION_ARB, gl_versions[i].major,
210 GLX_CONTEXT_MINOR_VERSION_ARB, gl_versions[i].minor,
211 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
212 None
213 };
214 fContext =
joshualittb59d1bc2016-01-20 08:07:01 -0800215 glXCreateContextAttribsARB(fDisplay, bestFbc, glxShareContext, True,
216 context_attribs_gl);
bsalomon@google.com373a6632011-10-19 20:43:20 +0000217
joshualittc863ab02014-08-07 13:48:49 -0700218 // Sync to ensure any errors generated are processed.
219 XSync(fDisplay, False);
220
221 if (!ctxErrorOccurred && fContext) {
222 break;
223 }
224 // try again
225 ctxErrorOccurred = false;
226 }
227
228 // Couldn't create GL 3.0 context.
229 // Fall back to old-style 2.x context.
230 // When a context version below 3.0 is requested,
231 // implementations will return the newest context version
232 // compatible with OpenGL versions less than version 3.0.
kkinnunen80549fc2014-06-30 06:36:31 -0700233 if (ctxErrorOccurred || !fContext) {
joshualittc863ab02014-08-07 13:48:49 -0700234 static const int context_attribs_gl_fallback[] = {
235 GLX_CONTEXT_MAJOR_VERSION_ARB, 1,
236 GLX_CONTEXT_MINOR_VERSION_ARB, 0,
237 None
238 };
bsalomon@google.com373a6632011-10-19 20:43:20 +0000239
kkinnunen80549fc2014-06-30 06:36:31 -0700240 ctxErrorOccurred = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000241
joshualittb59d1bc2016-01-20 08:07:01 -0800242 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, glxShareContext, True,
kkinnunen80549fc2014-06-30 06:36:31 -0700243 context_attribs_gl_fallback);
244 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000245 }
246 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000247
248 // Sync to ensure any errors generated are processed.
249 XSync(fDisplay, False);
250
251 // Restore the original error handler
252 XSetErrorHandler(oldHandler);
253
254 if (ctxErrorOccurred || !fContext) {
255 SkDebugf("Failed to create an OpenGL context.\n");
256 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -0700257 return;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000258 }
259
260 // Verify that context is a direct context
261 if (!glXIsDirect(fDisplay, fContext)) {
262 //SkDebugf("Indirect GLX rendering context obtained.\n");
263 } else {
264 //SkDebugf("Direct GLX rendering context obtained.\n");
265 }
266
267 //SkDebugf("Making context current.\n");
268 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) {
269 SkDebugf("Could not set the context.\n");
270 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -0700271 return;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000272 }
273
cdaltond416a5b2015-06-23 13:23:44 -0700274 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateNativeInterface());
halcanary96fcdcc2015-08-27 07:41:13 -0700275 if (nullptr == gl.get()) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000276 SkDebugf("Failed to create gl interface");
277 this->destroyGLContext();
kkinnunen30bc88c2014-10-15 23:03:54 -0700278 return;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000279 }
kkinnunen30bc88c2014-10-15 23:03:54 -0700280
cdaltond416a5b2015-06-23 13:23:44 -0700281 if (!gl->validate()) {
kkinnunen30bc88c2014-10-15 23:03:54 -0700282 SkDebugf("Failed to validate gl interface");
283 this->destroyGLContext();
284 return;
285 }
cdaltond416a5b2015-06-23 13:23:44 -0700286
mtklein18300a32016-03-16 13:53:35 -0700287 this->init(gl.release());
kkinnunen30bc88c2014-10-15 23:03:54 -0700288}
289
290
291GLXGLContext::~GLXGLContext() {
cdaltond416a5b2015-06-23 13:23:44 -0700292 this->teardown();
kkinnunen30bc88c2014-10-15 23:03:54 -0700293 this->destroyGLContext();
294}
295
296void GLXGLContext::destroyGLContext() {
kkinnunen30bc88c2014-10-15 23:03:54 -0700297 if (fDisplay) {
298 glXMakeCurrent(fDisplay, 0, 0);
299
300 if (fContext) {
301 glXDestroyContext(fDisplay, fContext);
halcanary96fcdcc2015-08-27 07:41:13 -0700302 fContext = nullptr;
kkinnunen30bc88c2014-10-15 23:03:54 -0700303 }
304
305 if (fGlxPixmap) {
306 glXDestroyGLXPixmap(fDisplay, fGlxPixmap);
307 fGlxPixmap = 0;
308 }
309
310 if (fPixmap) {
311 XFreePixmap(fDisplay, fPixmap);
312 fPixmap = 0;
313 }
314
315 XCloseDisplay(fDisplay);
halcanary96fcdcc2015-08-27 07:41:13 -0700316 fDisplay = nullptr;
kkinnunen30bc88c2014-10-15 23:03:54 -0700317 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000318}
319
cdaltond416a5b2015-06-23 13:23:44 -0700320void GLXGLContext::onPlatformMakeCurrent() const {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000321 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) {
322 SkDebugf("Could not set the context.\n");
323 }
324}
djsollen@google.comc9542ca2013-10-09 18:25:38 +0000325
cdaltond416a5b2015-06-23 13:23:44 -0700326void GLXGLContext::onPlatformSwapBuffers() const {
djsollen@google.comc9542ca2013-10-09 18:25:38 +0000327 glXSwapBuffers(fDisplay, fGlxPixmap);
328}
kkinnunen9e61bb72014-10-09 05:24:15 -0700329
cdaltond416a5b2015-06-23 13:23:44 -0700330GrGLFuncPtr GLXGLContext::onPlatformGetProcAddress(const char* procName) const {
331 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName));
332}
333
kkinnunen9e61bb72014-10-09 05:24:15 -0700334} // anonymous namespace
335
joshualittb59d1bc2016-01-20 08:07:01 -0800336SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI, SkGLContext* shareContext) {
337 GLXGLContext* glxShareContext = reinterpret_cast<GLXGLContext*>(shareContext);
338 GLXGLContext *ctx = new GLXGLContext(forcedGpuAPI, glxShareContext);
kkinnunen30bc88c2014-10-15 23:03:54 -0700339 if (!ctx->isValid()) {
halcanary385fe4d2015-08-26 13:07:48 -0700340 delete ctx;
halcanary96fcdcc2015-08-27 07:41:13 -0700341 return nullptr;
kkinnunen30bc88c2014-10-15 23:03:54 -0700342 }
343 return ctx;
kkinnunen9e61bb72014-10-09 05:24:15 -0700344}