Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/gpu/gl/glx/CreatePlatformGLTestContext_glx.cpp

Issue 2201033003: Check more GLES versions when creating context (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/03 17:31:55 is_es should be isES the "f" prefix is reserved fo
mkollaro 2016/08/04 13:26:24 Done.
59 GLXContext createBestContext(bool is_es, Display* fDisplay, GLXFBConfig best Fbc,
robertphillips 2016/08/03 17:31:55 Line this last parameter up with the opening '(' ?
mkollaro 2016/08/04 13:26:24 Done.
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
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))) {
robertphillips 2016/08/03 17:31:55 line the last parameter up with the opening '(' ?
mkollaro 2016/08/04 13:26:24 Done.
187 const int context_attribs_gles[] = { 175 fContext = createBestContext(true, fDisplay, bestFbc,
188 GLX_CONTEXT_MAJOR_VERSION_ARB, 2, 176 glxShareContext);
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 } 177 }
196 } else { 178 } else {
robertphillips 2016/08/03 17:31:56 Can this fit on one line?
mkollaro 2016/08/04 13:26:25 Done.
197 // Well, unfortunately GLX will not just give us the highest context so instead we have 179 fContext = createBestContext(false, fDisplay, bestFbc,
198 // to do this nastiness 180 glxShareContext);
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 } 181 }
249 } 182 }
250 183 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"); 184 SkDebugf("Failed to create an OpenGL context.\n");
259 this->destroyGLContext(); 185 this->destroyGLContext();
260 return; 186 return;
261 } 187 }
262 188
263 // Verify that context is a direct context 189 // Verify that context is a direct context
264 if (!glXIsDirect(fDisplay, fContext)) { 190 if (!glXIsDirect(fDisplay, fContext)) {
265 //SkDebugf("Indirect GLX rendering context obtained.\n"); 191 //SkDebugf("Indirect GLX rendering context obtained.\n");
266 } else { 192 } else {
267 //SkDebugf("Direct GLX rendering context obtained.\n"); 193 //SkDebugf("Direct GLX rendering context obtained.\n");
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 if (fPixmap) { 239 if (fPixmap) {
314 XFreePixmap(fDisplay, fPixmap); 240 XFreePixmap(fDisplay, fPixmap);
315 fPixmap = 0; 241 fPixmap = 0;
316 } 242 }
317 243
318 XCloseDisplay(fDisplay); 244 XCloseDisplay(fDisplay);
319 fDisplay = nullptr; 245 fDisplay = nullptr;
320 } 246 }
321 } 247 }
322 248
249 /* Create a context with the highest possible version.
250 *
251 * Disable Xlib errors for the duration of this function (by default they abort
252 * the program) and try to get a context starting from the highest version
253 * number - there is no way to just directly ask what the highest supported
254 * version is.
255 *
256 * Return nullptr upon failure.
257 */
robertphillips 2016/08/03 17:31:55 is_es & fDisplay again Can we make this a static
mkollaro 2016/08/04 13:26:25 I've made it a normal member and directly set fCon
robertphillips 2016/08/04 19:42:13 I think having it be a static method would be clea
258 GLXContext GLXGLTestContext::createBestContext(bool is_es, Display* fDisplay,
259 GLXFBConfig bestFbc, GLXContext glxShareContext) {
260 auto glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
261 glXGetProcAddressARB((GrGLubyte*)"glXCreateContextAttribsARB");
robertphillips 2016/08/03 17:31:56 either: nullptr == glXCreateContextAttribsARB or
mkollaro 2016/08/04 13:26:24 Done.
262 if (glXCreateContextAttribsARB == nullptr) {
263 SkDebugf("Failed to get address of glXCreateContextAttribsARB");
264 return nullptr;
265 }
266 // Install Xlib error handler that will set ctxErrorOccurred.
267 // WARNING: It is global for all threads.
268 ctxErrorOccurred = false;
robertphillips 2016/08/03 17:31:56 Can this fit on one line ?
mkollaro 2016/08/04 13:26:25 Done.
269 int (*oldHandler)(Display*, XErrorEvent*) =
270 XSetErrorHandler(&ctxErrorHandler);
271
robertphillips 2016/08/03 17:31:56 The 'f' prefix is reserved for member variables. T
mkollaro 2016/08/04 13:26:24 Done.
272 GLXContext fContext = nullptr;
273 auto versions = is_es ? gles_versions : gl_versions;
274 // Well, unfortunately GLX will not just give us the highest context so
275 // instead we have to do this nastiness
276 for (int i = versions.size() - 1; i >= 0 ; i--) {
robertphillips 2016/08/03 17:31:55 I believe the nVidia specific comment should be mo
mkollaro 2016/08/04 13:26:25 Done.
277 // TODO When Nvidia implements NVPR on Core profiles, we should start
278 // requesting core here - currently Nv Path rendering on Nvidia
279 // requires a compatibility profile.
280 // WARNING: Don't try to optimize this and make this array static. The
281 // glXCreateContextAttribsARB call writes to it upon failure and the
282 // next call would fail too.
283 std::vector<int> flags = {
284 GLX_CONTEXT_MAJOR_VERSION_ARB, versions[i].first,
285 GLX_CONTEXT_MINOR_VERSION_ARB, versions[i].second,
286 };
287 if (is_es) {
288 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB);
289 // the ES2 flag should work even for higher versions
290 flags.push_back(GLX_CONTEXT_ES2_PROFILE_BIT_EXT);
291 } else if (versions[i].first > 2) {
292 flags.push_back(GLX_CONTEXT_PROFILE_MASK_ARB);
293 flags.push_back(GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB);
294 }
295 flags.push_back(0);
296 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc,
297 glxShareContext, true, &flags[0]);
298 // Sync to ensure any errors generated are processed.
299 XSync(fDisplay, False);
300
301 if (!ctxErrorOccurred && fContext) {
robertphillips 2016/08/03 17:31:55 Remove this commented out code
mkollaro 2016/08/04 13:26:25 Done.
302 //SkDebugf("Created %d.%d context\n", versions[i].first,
303 //versions[i].second);
304 break;
305 }
306 // try again
307 ctxErrorOccurred = false;
308 }
309 // Restore the original error handler.
310 XSetErrorHandler(oldHandler);
311
312 return fContext;
313 }
314
323 void GLXGLTestContext::onPlatformMakeCurrent() const { 315 void GLXGLTestContext::onPlatformMakeCurrent() const {
324 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { 316 if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) {
325 SkDebugf("Could not set the context.\n"); 317 SkDebugf("Could not set the context.\n");
326 } 318 }
327 } 319 }
328 320
329 void GLXGLTestContext::onPlatformSwapBuffers() const { 321 void GLXGLTestContext::onPlatformSwapBuffers() const {
330 glXSwapBuffers(fDisplay, fGlxPixmap); 322 glXSwapBuffers(fDisplay, fGlxPixmap);
331 } 323 }
332 324
333 GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) con st { 325 GrGLFuncPtr GLXGLTestContext::onPlatformGetProcAddress(const char* procName) con st {
334 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName)); 326 return glXGetProcAddress(reinterpret_cast<const GLubyte*>(procName));
335 } 327 }
336 328
337 } // anonymous namespace 329 } // anonymous namespace
338 330
339 namespace sk_gpu_test { 331 namespace sk_gpu_test {
340 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, 332 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
341 GLTestContext *shareContext) { 333 GLTestContext *shareContext) {
342 GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(sha reContext); 334 GLXGLTestContext *glxShareContext = reinterpret_cast<GLXGLTestContext *>(sha reContext);
343 GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext); 335 GLXGLTestContext *ctx = new GLXGLTestContext(forcedGpuAPI, glxShareContext);
344 if (!ctx->isValid()) { 336 if (!ctx->isValid()) {
345 delete ctx; 337 delete ctx;
346 return nullptr; 338 return nullptr;
347 } 339 }
348 return ctx; 340 return ctx;
349 } 341 }
350 } // namespace sk_gpu_test 342 } // namespace sk_gpu_test
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698