OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "gl/SkGLContext.h" | 8 #include "gl/SkGLContext.h" |
9 | 9 |
10 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
11 #include <GL/glx.h> | 11 #include <GL/glx.h> |
12 #include <GL/glu.h> | 12 #include <GL/glu.h> |
13 | 13 |
14 #include "GrNativeDisplay_glx.h" | |
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 struct { int major, minor; } 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}, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 Display* fDisplay; | 62 Display* fDisplay; |
61 Pixmap fPixmap; | 63 Pixmap fPixmap; |
62 GLXPixmap fGlxPixmap; | 64 GLXPixmap fGlxPixmap; |
63 }; | 65 }; |
64 | 66 |
65 GLXGLContext::GLXGLContext(GrGLStandard forcedGpuAPI) | 67 GLXGLContext::GLXGLContext(GrGLStandard forcedGpuAPI) |
66 : fContext(nullptr) | 68 : fContext(nullptr) |
67 , fDisplay(nullptr) | 69 , fDisplay(nullptr) |
68 , fPixmap(0) | 70 , fPixmap(0) |
69 , fGlxPixmap(0) { | 71 , fGlxPixmap(0) { |
70 | |
71 fDisplay = XOpenDisplay(0); | 72 fDisplay = XOpenDisplay(0); |
72 | 73 |
73 if (!fDisplay) { | 74 if (!fDisplay) { |
74 SkDebugf("Failed to open X display.\n"); | 75 SkDebugf("Failed to open X display.\n"); |
75 this->destroyGLContext(); | 76 this->destroyGLContext(); |
76 return; | 77 return; |
77 } | 78 } |
78 | 79 |
79 // Get a matching FB config | 80 // Get a matching FB config |
robertphillips
2015/10/16 14:54:40
visualAttribs ?
static const ?
| |
80 static int visual_attribs[] = { | 81 static int visual_attribs[] = { |
81 GLX_X_RENDERABLE , True, | 82 GLX_X_RENDERABLE , True, |
82 GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT, | 83 GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT, |
83 None | 84 None |
84 }; | 85 }; |
85 | 86 |
86 int glx_major, glx_minor; | 87 GLXFBConfig bestFbConfig; |
87 | 88 if (!GrNativeDisplay::Initialize(visual_attribs, fDisplay, &bestFbConfig)) { |
88 // FBConfigs were added in GLX version 1.3. | |
89 if (!glXQueryVersion(fDisplay, &glx_major, &glx_minor) || | |
90 ((glx_major == 1) && (glx_minor < 3)) || (glx_major < 1)) { | |
91 SkDebugf("GLX version 1.3 or higher required.\n"); | |
92 this->destroyGLContext(); | 89 this->destroyGLContext(); |
93 return; | 90 return; |
94 } | 91 } |
95 | 92 |
96 //SkDebugf("Getting matching framebuffer configs.\n"); | |
97 int fbcount; | |
98 GLXFBConfig *fbc = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay), | |
99 visual_attribs, &fbcount); | |
100 if (!fbc) { | |
101 SkDebugf("Failed to retrieve a framebuffer config.\n"); | |
102 this->destroyGLContext(); | |
103 return; | |
104 } | |
105 //SkDebugf("Found %d matching FB configs.\n", fbcount); | |
106 | |
107 // Pick the FB config/visual with the most samples per pixel | |
108 //SkDebugf("Getting XVisualInfos.\n"); | |
109 int best_fbc = -1, best_num_samp = -1; | |
110 | |
111 int i; | |
112 for (i = 0; i < fbcount; ++i) { | |
113 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, fbc[i]); | |
114 if (vi) { | |
115 int samp_buf, samples; | |
116 glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf ); | |
117 glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLES, &samples); | |
118 | |
119 //SkDebugf(" Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d," | |
120 // " SAMPLES = %d\n", | |
121 // i, (unsigned int)vi->visualid, samp_buf, samples); | |
122 | |
123 if (best_fbc < 0 || (samp_buf && samples > best_num_samp)) | |
124 best_fbc = i, best_num_samp = samples; | |
125 } | |
126 XFree(vi); | |
127 } | |
128 | |
129 GLXFBConfig bestFbc = fbc[best_fbc]; | |
130 | |
131 // Be sure to free the FBConfig list allocated by glXChooseFBConfig() | |
132 XFree(fbc); | |
133 | |
134 // Get a visual | 93 // Get a visual |
135 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, bestFbc); | 94 XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, bestFbConfig); |
136 //SkDebugf("Chosen visual ID = 0x%x\n", (unsigned int)vi->visualid); | 95 //SkDebugf("Chosen visual ID = 0x%x\n", (unsigned int)vi->visualid); |
137 | 96 |
138 fPixmap = XCreatePixmap(fDisplay, RootWindow(fDisplay, vi->screen), 10, 10, vi->depth); | 97 fPixmap = XCreatePixmap(fDisplay, RootWindow(fDisplay, vi->screen), 10, 10, vi->depth); |
139 | 98 |
140 if (!fPixmap) { | 99 if (!fPixmap) { |
141 SkDebugf("Failed to create pixmap.\n"); | 100 SkDebugf("Failed to create pixmap.\n"); |
142 this->destroyGLContext(); | 101 this->destroyGLContext(); |
143 return; | 102 return; |
144 } | 103 } |
145 | 104 |
146 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); | 105 fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); |
147 | 106 |
148 // Done with the visual info data | 107 // Done with the visual info data |
149 XFree(vi); | 108 XFree(vi); |
150 | 109 |
151 // Create the context | 110 // Create the context |
152 | 111 |
153 // Install an X error handler so the application won't exit if GL 3.0 | 112 // Install an X error handler so the application won't exit if GL 3.0 |
154 // context allocation fails. | 113 // context allocation fails. |
155 // | 114 // |
156 // Note this error handler is global. | 115 // Note this error handler is global. |
157 // All display connections in all threads of a process use the same | 116 // All display connections in all threads of a process use the same |
158 // error handler, so be sure to guard against other threads issuing | 117 // error handler, so be sure to guard against other threads issuing |
159 // X commands while this code is running. | 118 // X commands while this code is running. |
160 ctxErrorOccurred = false; | 119 ctxErrorOccurred = false; |
161 int (*oldHandler)(Display*, XErrorEvent*) = | 120 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandle r); |
162 XSetErrorHandler(&ctxErrorHandler); | |
163 | 121 |
164 // Get the default screen's GLX extension list | 122 // Get the default screen's GLX extension list |
165 const char *glxExts = glXQueryExtensionsString( | 123 const char *glxExts = glXQueryExtensionsString(fDisplay, DefaultScreen(fDisp lay)); |
166 fDisplay, DefaultScreen(fDisplay) | |
167 ); | |
168 | 124 |
169 | 125 |
170 // Check for the GLX_ARB_create_context extension string and the function. | 126 // Check for the GLX_ARB_create_context extension string and the function. |
171 // If either is not present, use GLX 1.3 context creation method. | 127 // If either is not present, use GLX 1.3 context creation method. |
172 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_cont ext"), | 128 if (!gluCheckExtension(reinterpret_cast<const GLubyte*>("GLX_ARB_create_cont ext"), |
173 reinterpret_cast<const GLubyte*>(glxExts))) { | 129 reinterpret_cast<const GLubyte*>(glxExts))) { |
174 if (kGLES_GrGLStandard != forcedGpuAPI) { | 130 if (kGLES_GrGLStandard != forcedGpuAPI) { |
175 fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0, True); | 131 fContext = glXCreateNewContext(fDisplay, bestFbConfig, GLX_RGBA_TYPE , 0, True); |
176 } | 132 } |
177 } else { | 133 } else { |
178 //SkDebugf("Creating context.\n"); | 134 //SkDebugf("Creating context.\n"); |
179 PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = | 135 PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = |
180 (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((GrGLubyte* )"glXCreateContextAttribsARB"); | 136 (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((GrGLubyte* )"glXCreateContextAttribsARB"); |
181 | 137 |
182 if (kGLES_GrGLStandard == forcedGpuAPI) { | 138 if (kGLES_GrGLStandard == forcedGpuAPI) { |
183 if (gluCheckExtension( | 139 if (gluCheckExtension( |
184 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2 _profile"), | 140 reinterpret_cast<const GLubyte*>("GLX_EXT_create_context_es2 _profile"), |
185 reinterpret_cast<const GLubyte*>(glxExts))) { | 141 reinterpret_cast<const GLubyte*>(glxExts))) { |
186 static const int context_attribs_gles[] = { | 142 static const int context_attribs_gles[] = { |
187 GLX_CONTEXT_MAJOR_VERSION_ARB, 3, | 143 GLX_CONTEXT_MAJOR_VERSION_ARB, 3, |
188 GLX_CONTEXT_MINOR_VERSION_ARB, 0, | 144 GLX_CONTEXT_MINOR_VERSION_ARB, 0, |
189 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EX T, | 145 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EX T, |
190 None | 146 None |
191 }; | 147 }; |
192 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, 0, True , | 148 fContext = glXCreateContextAttribsARB(fDisplay, bestFbConfig, 0, True, |
193 context_attribs_gles); | 149 context_attribs_gles); |
194 } | 150 } |
195 } else { | 151 } else { |
196 // Well, unfortunately GLX will not just give us the highest context so instead we have | 152 // Well, unfortunately GLX will not just give us the highest context so instead we have |
197 // to do this nastiness | 153 // to do this nastiness |
198 for (i = NUM_GL_VERSIONS - 2; i > 0 ; i--) { | 154 for (int i = NUM_GL_VERSIONS - 2; i > 0 ; i--) { |
199 /* don't bother below GL 3.0 */ | 155 /* don't bother below GL 3.0 */ |
200 if (gl_versions[i].major == 3 && gl_versions[i].minor == 0) { | 156 if (gl_versions[i].major == 3 && gl_versions[i].minor == 0) { |
201 break; | 157 break; |
202 } | 158 } |
203 // On Nvidia GPUs, to use Nv Path rendering we need a compatibil ity profile for the | 159 // On Nvidia GPUs, to use Nv Path rendering we need a compatibil ity profile for the |
204 // time being. | 160 // time being. |
205 // TODO when Nvidia implements NVPR on Core profiles, we should start requesting | 161 // TODO when Nvidia implements NVPR on Core profiles, we should start requesting |
206 // core here | 162 // core here |
207 static const int context_attribs_gl[] = { | 163 static const int context_attribs_gl[] = { |
208 GLX_CONTEXT_MAJOR_VERSION_ARB, gl_versions[i].major, | 164 GLX_CONTEXT_MAJOR_VERSION_ARB, gl_versions[i].major, |
209 GLX_CONTEXT_MINOR_VERSION_ARB, gl_versions[i].minor, | 165 GLX_CONTEXT_MINOR_VERSION_ARB, gl_versions[i].minor, |
210 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PR OFILE_BIT_ARB, | 166 GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PR OFILE_BIT_ARB, |
211 None | 167 None |
212 }; | 168 }; |
213 fContext = | 169 fContext = |
214 glXCreateContextAttribsARB(fDisplay, bestFbc, 0, True, c ontext_attribs_gl); | 170 glXCreateContextAttribsARB(fDisplay, bestFbConfig, 0, Tr ue, |
171 context_attribs_gl); | |
215 | 172 |
216 // Sync to ensure any errors generated are processed. | 173 // Sync to ensure any errors generated are processed. |
217 XSync(fDisplay, False); | 174 XSync(fDisplay, False); |
218 | 175 |
219 if (!ctxErrorOccurred && fContext) { | 176 if (!ctxErrorOccurred && fContext) { |
220 break; | 177 break; |
221 } | 178 } |
222 // try again | 179 // try again |
223 ctxErrorOccurred = false; | 180 ctxErrorOccurred = false; |
224 } | 181 } |
225 | 182 |
226 // Couldn't create GL 3.0 context. | 183 // Couldn't create GL 3.0 context. |
227 // Fall back to old-style 2.x context. | 184 // Fall back to old-style 2.x context. |
228 // When a context version below 3.0 is requested, | 185 // When a context version below 3.0 is requested, |
229 // implementations will return the newest context version | 186 // implementations will return the newest context version |
230 // compatible with OpenGL versions less than version 3.0. | 187 // compatible with OpenGL versions less than version 3.0. |
231 if (ctxErrorOccurred || !fContext) { | 188 if (ctxErrorOccurred || !fContext) { |
232 static const int context_attribs_gl_fallback[] = { | 189 static const int context_attribs_gl_fallback[] = { |
233 GLX_CONTEXT_MAJOR_VERSION_ARB, 1, | 190 GLX_CONTEXT_MAJOR_VERSION_ARB, 1, |
234 GLX_CONTEXT_MINOR_VERSION_ARB, 0, | 191 GLX_CONTEXT_MINOR_VERSION_ARB, 0, |
235 None | 192 None |
236 }; | 193 }; |
237 | 194 |
238 ctxErrorOccurred = false; | 195 ctxErrorOccurred = false; |
239 | 196 |
240 fContext = glXCreateContextAttribsARB(fDisplay, bestFbc, 0, True , | 197 fContext = glXCreateContextAttribsARB(fDisplay, bestFbConfig, 0, True, |
241 context_attribs_gl_fallbac k); | 198 context_attribs_gl_fallbac k); |
242 } | 199 } |
243 } | 200 } |
244 } | 201 } |
245 | 202 |
246 // Sync to ensure any errors generated are processed. | 203 // Sync to ensure any errors generated are processed. |
247 XSync(fDisplay, False); | 204 XSync(fDisplay, False); |
248 | 205 |
249 // Restore the original error handler | 206 // Restore the original error handler |
250 XSetErrorHandler(oldHandler); | 207 XSetErrorHandler(oldHandler); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 } // anonymous namespace | 289 } // anonymous namespace |
333 | 290 |
334 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) { | 291 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) { |
335 GLXGLContext *ctx = new GLXGLContext(forcedGpuAPI); | 292 GLXGLContext *ctx = new GLXGLContext(forcedGpuAPI); |
336 if (!ctx->isValid()) { | 293 if (!ctx->isValid()) { |
337 delete ctx; | 294 delete ctx; |
338 return nullptr; | 295 return nullptr; |
339 } | 296 } |
340 return ctx; | 297 return ctx; |
341 } | 298 } |
OLD | NEW |