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(); |
robertphillips
2016/08/04 19:42:13
rename is_es to isES
mkollaro
2016/08/08 07:56:53
Done.
| |
59 void createBestContext(bool is_es, GLXFBConfig bestFbc, GLXContext glxShared Context); | |
54 | 60 |
55 void onPlatformMakeCurrent() const override; | 61 void onPlatformMakeCurrent() const override; |
56 void onPlatformSwapBuffers() const override; | 62 void onPlatformSwapBuffers() const override; |
57 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; | 63 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; |
58 | 64 |
59 GLXContext fContext; | 65 GLXContext fContext; |
60 Display* fDisplay; | 66 Display* fDisplay; |
61 Pixmap fPixmap; | 67 Pixmap fPixmap; |
62 GLXPixmap fGlxPixmap; | 68 GLXPixmap fGlxPixmap; |
63 }; | 69 }; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 SkDebugf("Failed to create pixmap.\n"); | 148 SkDebugf("Failed to create pixmap.\n"); |
143 this->destroyGLContext(); | 149 this->destroyGLContext(); |
144 return; | 150 return; |
145 } | 151 } |
146 | 152 |
147 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); | 153 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); |
148 | 154 |
149 // Done with the visual info data | 155 // Done with the visual info data |
150 XFree(vi); | 156 XFree(vi); |
151 | 157 |
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 | 158 // Get the default screen's GLX extension list |
166 const char *glxExts = glXQueryExtensionsString( | 159 const char *glxExts = glXQueryExtensionsString( |
167 fDisplay, DefaultScreen(fDisplay) | 160 fDisplay, DefaultScreen(fDisplay) |
168 ); | 161 ); |
169 | |
170 | |
171 // Check for the GLX_ARB_create_context extension string and the function. | 162 // 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. | 163 // If either is not present, use GLX 1.3 context creation method. |
173 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_cont ext"), | 164 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_cont ext"), |
174 reinterpret_cast<const GLubyte*>(glxExts))) { | 165 reinterpret_cast<const GLubyte*>(glxExts))) { |
175 if (kGLES_GrGLStandard != forcedGpuAPI) { | 166 if (kGLES_GrGLStandard != forcedGpuAPI) { |
176 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0, True); | 167 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0, True); |
177 } | 168 } |
178 } else { | 169 } else { |
179 //SkDebugf("Creating context.\n"); | |
180 PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = | |
181 (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((GrGLubyte* )"glXCreateContextAttribsARB"); | |
182 | |
183 if (kGLES_GrGLStandard == forcedGpuAPI) { | 170 if (kGLES_GrGLStandard == forcedGpuAPI) { |
184 if (gluCheckExtension( | 171 if (gluCheckExtension( |
185 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2 _profile"), | 172 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2 _profile"), |
186 reinterpret_cast<const GLubyte*>(glxExts))) { | 173 reinterpret_cast<const GLubyte*>(glxExts))) { |
187 const int context_attribs_gles[] = { | 174 this->createBestContext(true, bestFbc, glxShareContext); |
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 } | 175 } |
196 } else { | 176 } else { |
197 // Well, unfortunately GLX will not just give us the highest context so instead we have | 177 this->createBestContext(false, bestFbc, glxShareContext); |
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 } | 178 } |
249 } | 179 } |
250 | 180 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"); | 181 SkDebugf("Failed to create an OpenGL context.\n"); |
259 this->destroyGLContext(); | 182 this->destroyGLContext(); |
260 return; | 183 return; |
261 } | 184 } |
262 | 185 |
263 // Verify that context is a direct context | 186 // Verify that context is a direct context |
264 if (!glXIsDirect(fDisplay, fContext)) { | 187 if (!glXIsDirect(fDisplay, fContext)) { |
265 //SkDebugf("Indirect GLX rendering context obtained.\n"); | 188 //SkDebugf("Indirect GLX rendering context obtained.\n"); |
266 } else { | 189 } else { |
267 //SkDebugf("Direct GLX rendering context obtained.\n"); | 190 //SkDebugf("Direct GLX rendering context obtained.\n"); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 if (fPixmap) { | 236 if (fPixmap) { |
314 XFreePixmap(fDisplay, fPixmap); | 237 XFreePixmap(fDisplay, fPixmap); |
315 fPixmap = 0; | 238 fPixmap = 0; |
316 } | 239 } |
317 | 240 |
318 XCloseDisplay(fDisplay); | 241 XCloseDisplay(fDisplay); |
319 fDisplay = nullptr; | 242 fDisplay = nullptr; |
320 } | 243 } |
321 } | 244 } |
322 | 245 |
246 /* Create a context with the highest possible version. | |
247 * | |
248 * Disable Xlib errors for the duration of this function (by default they abort | |
249 * the program) and try to get a context starting from the highest version | |
250 * number - there is no way to just directly ask what the highest supported | |
251 * version is. | |
252 * | |
253 * Sets the correct context into fContext. | |
254 */ | |
robertphillips
2016/08/04 19:42:13
rename is_es to isES
mkollaro
2016/08/08 07:56:53
Done.
| |
255 void GLXGLTestContext::createBestContext(bool is_es, GLXFBConfig bestFbc, | |
256 GLXContext glxShareContext) { | |
257 fContext = nullptr; | |
258 auto glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) | |
259 glXGetProcAddressARB((GrGLubyte*)"glXCreateContextAttribsARB"); | |
260 if (!glXCreateContextAttribsARB) { | |
261 SkDebugf("Failed to get address of glXCreateContextAttribsARB"); | |
262 return; | |
263 } | |
264 // Install Xlib error handler that will set ctxErrorOccurred. | |
265 // WARNING: It is global for all threads. | |
266 ctxErrorOccurred = false; | |
267 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandle r); | |
268 | |
269 auto versions = is_es ? gles_versions : gl_versions; | |
270 // Well, unfortunately GLX will not just give us the highest context so | |
271 // instead we have to do this nastiness | |
272 for (int i = versions.size() - 1; i >= 0 ; i--) { | |
273 // WARNING: Don't try to optimize this and make this array static. The | |
274 // glXCreateContextAttribsARB call writes to it upon failure and the | |
275 // next call would fail too. | |
276 std::vector<int> flags = { | |
277 GLX_CONTEXT_MAJOR_VERSION_ARB, versions[i].first, | |
278 GLX_CONTEXT_MINOR_VERSION_ARB, versions[i].second, | |
279 }; | |
280 if (is_es) { | |
281 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); | |
282 // the ES2 flag should work even for higher versions | |
283 flags.push_back(GLX_CONTEXT_ES2_PROFILE_BIT_EXT); | |
284 } else if (versions[i].first > 2) { | |
285 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); | |
286 // TODO When Nvidia implements NVPR on Core profiles, we should star t | |
287 // requesting core here - currently Nv Path rendering on Nvidia | |
288 // requires a compatibility profile. | |
289 flags.push_back(GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB); | |
290 } | |
291 flags.push_back(0); | |
292 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, glxShareContext , true, | |
293 &flags[0]); | |
294 // Sync to ensure any errors generated are processed. | |
295 XSync(fDisplay, False); | |
296 | |
297 if (!ctxErrorOccurred && fContext) { | |
298 break; | |
299 } | |
300 // try again | |
301 ctxErrorOccurred = false; | |
302 } | |
303 // Restore the original error handler. | |
304 XSetErrorHandler(oldHandler); | |
305 } | |
306 | |
323 void GLXGLTestContext::onPlatformMakeCurrent() const { | 307 void GLXGLTestContext::onPlatformMakeCurrent() const { |
324 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { | 308 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { |
325 SkDebugf("Could not set the context.\n"); | 309 SkDebugf("Could not set the context.\n"); |
326 } | 310 } |
327 } | 311 } |
328 | 312 |
329 void GLXGLTestContext::onPlatformSwapBuffers() const { | 313 void GLXGLTestContext::onPlatformSwapBuffers() const { |
330 glXSwapBuffers(fDisplay, fGlxPixmap); | 314 glXSwapBuffers(fDisplay, fGlxPixmap); |
331 } | 315 } |
332 | 316 |
333 GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) con st { | 317 GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) con st { |
334 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName)); | 318 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName)); |
335 } | 319 } |
336 | 320 |
337 } // anonymous namespace | 321 } // anonymous namespace |
338 | 322 |
339 namespace sk_gpu_test { | 323 namespace sk_gpu_test { |
340 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, | 324 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, |
341 GLTestContext *shareContext) { | 325 GLTestContext *shareContext) { |
342 GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(sha reContext); | 326 GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(sha reContext); |
343 GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext); | 327 GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext); |
344 if (!ctx->isValid()) { | 328 if (!ctx->isValid()) { |
345 delete ctx; | 329 delete ctx; |
346 return nullptr; | 330 return nullptr; |
347 } | 331 } |
348 return ctx; | 332 return ctx; |
349 } | 333 } |
350 } // namespace sk_gpu_test | 334 } // namespace sk_gpu_test |
OLD | NEW |