OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 #include "gl/GLTestContext.h" | 7 #include "gl/GLTestContext.h" |
9 | 8 |
10 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
11 #include <GL/glx.h> | 10 #include <GL/glx.h> |
12 #include <GL/glu.h> | 11 #include <GL/glu.h> |
13 | 12 |
| 13 #include <vector> |
| 14 #include <utility> |
| 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 /* Note: Skia requires glx 1.3 or newer */ | 18 /* Note: Skia requires glx 1.3 or newer */ |
17 | 19 |
18 /* This struct is taken from a mesa demo. Please update as required */ | 20 /* This struct is taken from a mesa demo. Please update as required */ |
19 static const struct { int major, minor; } gl_versions[] = { | 21 static const std::vector<std::pair<int, int>> gl_versions = { |
20 {1, 0}, | 22 {1, 0}, |
21 {1, 1}, | 23 {1, 1}, |
22 {1, 2}, | 24 {1, 2}, |
23 {1, 3}, | 25 {1, 3}, |
24 {1, 4}, | 26 {1, 4}, |
25 {1, 5}, | 27 {1, 5}, |
26 {2, 0}, | 28 {2, 0}, |
27 {2, 1}, | 29 {2, 1}, |
28 {3, 0}, | 30 {3, 0}, |
29 {3, 1}, | 31 {3, 1}, |
30 {3, 2}, | 32 {3, 2}, |
31 {3, 3}, | 33 {3, 3}, |
32 {4, 0}, | 34 {4, 0}, |
33 {4, 1}, | 35 {4, 1}, |
34 {4, 2}, | 36 {4, 2}, |
35 {4, 3}, | 37 {4, 3}, |
36 {4, 4}, | 38 {4, 4}, |
37 {0, 0} /* end of list */ | |
38 }; | 39 }; |
39 #define NUM_GL_VERSIONS SK_ARRAY_COUNT(gl_versions) | 40 |
| 41 static const std::vector<std::pair<int, int>> gles_versions = { |
| 42 {2, 0}, |
| 43 {3, 0}, |
| 44 }; |
40 | 45 |
41 static bool ctxErrorOccurred = false; | 46 static bool ctxErrorOccurred = false; |
42 static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) { | 47 static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) { |
43 ctxErrorOccurred = true; | 48 ctxErrorOccurred = true; |
44 return 0; | 49 return 0; |
45 } | 50 } |
46 | 51 |
47 class GLXGLTestContext : public sk_gpu_test::GLTestContext { | 52 class GLXGLTestContext : public sk_gpu_test::GLTestContext { |
48 public: | 53 public: |
49 GLXGLTestContext(GrGLStandard forcedGpuAPI, GLXGLTestContext* shareList); | 54 GLXGLTestContext(GrGLStandard forcedGpuAPI, GLXGLTestContext* shareList); |
50 ~GLXGLTestContext() override; | 55 ~GLXGLTestContext() override; |
51 | 56 |
52 private: | 57 private: |
53 void destroyGLContext(); | 58 void destroyGLContext(); |
| 59 static GLXContext CreateBestContext(bool isES, Display* display, GLXFBConfig
bestFbc, |
| 60 GLXContext glxSharedContext); |
54 | 61 |
55 void onPlatformMakeCurrent() const override; | 62 void onPlatformMakeCurrent() const override; |
56 void onPlatformSwapBuffers() const override; | 63 void onPlatformSwapBuffers() const override; |
57 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; | 64 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; |
58 | 65 |
59 GLXContext fContext; | 66 GLXContext fContext; |
60 Display* fDisplay; | 67 Display* fDisplay; |
61 Pixmap fPixmap; | 68 Pixmap fPixmap; |
62 GLXPixmap fGlxPixmap; | 69 GLXPixmap fGlxPixmap; |
63 }; | 70 }; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 SkDebugf("Failed to create pixmap.\n"); | 149 SkDebugf("Failed to create pixmap.\n"); |
143 this->destroyGLContext(); | 150 this->destroyGLContext(); |
144 return; | 151 return; |
145 } | 152 } |
146 | 153 |
147 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); | 154 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); |
148 | 155 |
149 // Done with the visual info data | 156 // Done with the visual info data |
150 XFree(vi); | 157 XFree(vi); |
151 | 158 |
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 | 159 // Get the default screen's GLX extension list |
166 const char *glxExts = glXQueryExtensionsString( | 160 const char *glxExts = glXQueryExtensionsString( |
167 fDisplay, DefaultScreen(fDisplay) | 161 fDisplay, DefaultScreen(fDisplay) |
168 ); | 162 ); |
169 | |
170 | |
171 // Check for the GLX_ARB_create_context extension string and the function. | 163 // 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. | 164 // If either is not present, use GLX 1.3 context creation method. |
173 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_cont
ext"), | 165 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_cont
ext"), |
174 reinterpret_cast<const GLubyte*>(glxExts))) { | 166 reinterpret_cast<const GLubyte*>(glxExts))) { |
175 if (kGLES_GrGLStandard != forcedGpuAPI) { | 167 if (kGLES_GrGLStandard != forcedGpuAPI) { |
176 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0,
True); | 168 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0,
True); |
177 } | 169 } |
178 } else { | 170 } else { |
179 //SkDebugf("Creating context.\n"); | |
180 PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = | |
181 (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((GrGLubyte*
)"glXCreateContextAttribsARB"); | |
182 | |
183 if (kGLES_GrGLStandard == forcedGpuAPI) { | 171 if (kGLES_GrGLStandard == forcedGpuAPI) { |
184 if (gluCheckExtension( | 172 if (gluCheckExtension( |
185 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2
_profile"), | 173 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2
_profile"), |
186 reinterpret_cast<const GLubyte*>(glxExts))) { | 174 reinterpret_cast<const GLubyte*>(glxExts))) { |
187 const int context_attribs_gles[] = { | 175 fContext = CreateBestContext(true, fDisplay, bestFbc, glxShareCo
ntext); |
188 GLX_CONTEXT_MAJOR_VERSION_ARB, 2, | |
189 GLX_CONTEXT_MINOR_VERSION_ARB, 0, | |
190 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EX
T, | |
191 None | |
192 }; | |
193 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, glxShar
eContext, True, | |
194 context_attribs_gles); | |
195 } | 176 } |
196 } else { | 177 } else { |
197 // Well, unfortunately GLX will not just give us the highest context
so instead we have | 178 fContext = CreateBestContext(false, fDisplay, bestFbc, glxShareConte
xt); |
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) { | |
202 break; | |
203 } | |
204 // On Nvidia GPUs, to use Nv Path rendering we need a compatibil
ity profile for the | |
205 // time being. | |
206 // TODO when Nvidia implements NVPR on Core profiles, we should
start requesting | |
207 // core here | |
208 // Warning: This array should not be set to static. The | |
209 // glXCreateContextAttribsARB call writes to it upon failure and | |
210 // the next call would fail too. | |
211 const int context_attribs_gl[] = { | |
212 GLX_CONTEXT_MAJOR_VERSION_ARB, gl_versions[i].major, | |
213 GLX_CONTEXT_MINOR_VERSION_ARB, gl_versions[i].minor, | |
214 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PR
OFILE_BIT_ARB, | |
215 None | |
216 }; | |
217 fContext = | |
218 glXCreateContextAttribsARB(fDisplay, bestFbc, glxShareCo
ntext, True, | |
219 context_attribs_gl); | |
220 | |
221 // Sync to ensure any errors generated are processed. | |
222 XSync(fDisplay, False); | |
223 | |
224 if (!ctxErrorOccurred && fContext) { | |
225 break; | |
226 } | |
227 // try again | |
228 ctxErrorOccurred = false; | |
229 } | |
230 | |
231 // Couldn't create GL 3.0 context. | |
232 // Fall back to old-style 2.x context. | |
233 // When a context version below 3.0 is requested, | |
234 // implementations will return the newest context version | |
235 // compatible with OpenGL versions less than version 3.0. | |
236 if (ctxErrorOccurred || !fContext) { | |
237 const int context_attribs_gl_fallback[] = { | |
238 GLX_CONTEXT_MAJOR_VERSION_ARB, 1, | |
239 GLX_CONTEXT_MINOR_VERSION_ARB, 0, | |
240 None | |
241 }; | |
242 | |
243 ctxErrorOccurred = false; | |
244 | |
245 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, glxShar
eContext, True, | |
246 context_attribs_gl_fallbac
k); | |
247 } | |
248 } | 179 } |
249 } | 180 } |
250 | 181 if (!fContext) { |
251 // Sync to ensure any errors generated are processed. | |
252 XSync(fDisplay, False); | |
253 | |
254 // Restore the original error handler | |
255 XSetErrorHandler(oldHandler); | |
256 | |
257 if (ctxErrorOccurred || !fContext) { | |
258 SkDebugf("Failed to create an OpenGL context.\n"); | 182 SkDebugf("Failed to create an OpenGL context.\n"); |
259 this->destroyGLContext(); | 183 this->destroyGLContext(); |
260 return; | 184 return; |
261 } | 185 } |
262 | 186 |
263 // Verify that context is a direct context | 187 // Verify that context is a direct context |
264 if (!glXIsDirect(fDisplay, fContext)) { | 188 if (!glXIsDirect(fDisplay, fContext)) { |
265 //SkDebugf("Indirect GLX rendering context obtained.\n"); | 189 //SkDebugf("Indirect GLX rendering context obtained.\n"); |
266 } else { | 190 } else { |
267 //SkDebugf("Direct GLX rendering context obtained.\n"); | 191 //SkDebugf("Direct GLX rendering context obtained.\n"); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 if (fPixmap) { | 237 if (fPixmap) { |
314 XFreePixmap(fDisplay, fPixmap); | 238 XFreePixmap(fDisplay, fPixmap); |
315 fPixmap = 0; | 239 fPixmap = 0; |
316 } | 240 } |
317 | 241 |
318 XCloseDisplay(fDisplay); | 242 XCloseDisplay(fDisplay); |
319 fDisplay = nullptr; | 243 fDisplay = nullptr; |
320 } | 244 } |
321 } | 245 } |
322 | 246 |
| 247 /* Create a context with the highest possible version. |
| 248 * |
| 249 * Disable Xlib errors for the duration of this function (by default they abort |
| 250 * the program) and try to get a context starting from the highest version |
| 251 * number - there is no way to just directly ask what the highest supported |
| 252 * version is. |
| 253 * |
| 254 * Returns the correct context or NULL on failure. |
| 255 */ |
| 256 GLXContext GLXGLTestContext::CreateBestContext(bool isES, Display* display, GLXF
BConfig bestFbc, |
| 257 GLXContext glxShareContext) { |
| 258 auto glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) |
| 259 glXGetProcAddressARB((GrGLubyte*)"glXCreateContextAttribsARB"); |
| 260 if (!glXCreateContextAttribsARB) { |
| 261 SkDebugf("Failed to get address of glXCreateContextAttribsARB"); |
| 262 return nullptr; |
| 263 } |
| 264 GLXContext context = nullptr; |
| 265 // Install Xlib error handler that will set ctxErrorOccurred. |
| 266 // WARNING: It is global for all threads. |
| 267 ctxErrorOccurred = false; |
| 268 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandle
r); |
| 269 |
| 270 auto versions = isES ? gles_versions : gl_versions; |
| 271 // Well, unfortunately GLX will not just give us the highest context so |
| 272 // instead we have to do this nastiness |
| 273 for (int i = versions.size() - 1; i >= 0 ; i--) { |
| 274 // WARNING: Don't try to optimize this and make this array static. The |
| 275 // glXCreateContextAttribsARB call writes to it upon failure and the |
| 276 // next call would fail too. |
| 277 std::vector<int> flags = { |
| 278 GLX_CONTEXT_MAJOR_VERSION_ARB, versions[i].first, |
| 279 GLX_CONTEXT_MINOR_VERSION_ARB, versions[i].second, |
| 280 }; |
| 281 if (isES) { |
| 282 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); |
| 283 // the ES2 flag should work even for higher versions |
| 284 flags.push_back(GLX_CONTEXT_ES2_PROFILE_BIT_EXT); |
| 285 } else if (versions[i].first > 2) { |
| 286 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); |
| 287 // TODO When Nvidia implements NVPR on Core profiles, we should star
t |
| 288 // requesting core here - currently Nv Path rendering on Nvidia |
| 289 // requires a compatibility profile. |
| 290 flags.push_back(GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB); |
| 291 } |
| 292 flags.push_back(0); |
| 293 context = glXCreateContextAttribsARB(display, bestFbc, glxShareContext,
true, |
| 294 &flags[0]); |
| 295 // Sync to ensure any errors generated are processed. |
| 296 XSync(display, False); |
| 297 |
| 298 if (!ctxErrorOccurred && context) { |
| 299 break; |
| 300 } |
| 301 // try again |
| 302 ctxErrorOccurred = false; |
| 303 } |
| 304 // Restore the original error handler. |
| 305 XSetErrorHandler(oldHandler); |
| 306 return context; |
| 307 } |
| 308 |
323 void GLXGLTestContext::onPlatformMakeCurrent() const { | 309 void GLXGLTestContext::onPlatformMakeCurrent() const { |
324 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { | 310 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { |
325 SkDebugf("Could not set the context.\n"); | 311 SkDebugf("Could not set the context.\n"); |
326 } | 312 } |
327 } | 313 } |
328 | 314 |
329 void GLXGLTestContext::onPlatformSwapBuffers() const { | 315 void GLXGLTestContext::onPlatformSwapBuffers() const { |
330 glXSwapBuffers(fDisplay, fGlxPixmap); | 316 glXSwapBuffers(fDisplay, fGlxPixmap); |
331 } | 317 } |
332 | 318 |
333 GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) con
st { | 319 GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) con
st { |
334 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName)); | 320 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName)); |
335 } | 321 } |
336 | 322 |
337 } // anonymous namespace | 323 } // anonymous namespace |
338 | 324 |
339 namespace sk_gpu_test { | 325 namespace sk_gpu_test { |
340 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, | 326 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, |
341 GLTestContext *shareContext) { | 327 GLTestContext *shareContext) { |
342 GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(sha
reContext); | 328 GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(sha
reContext); |
343 GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext); | 329 GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext); |
344 if (!ctx->isValid()) { | 330 if (!ctx->isValid()) { |
345 delete ctx; | 331 delete ctx; |
346 return nullptr; | 332 return nullptr; |
347 } | 333 } |
348 return ctx; | 334 return ctx; |
349 } | 335 } |
350 } // namespace sk_gpu_test | 336 } // namespace sk_gpu_test |
OLD | NEW |