Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | #include "gl/SkNativeGLContext.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <GL/glu.h> |
michael@0 | 11 | |
michael@0 | 12 | #define GLX_1_3 1 |
michael@0 | 13 | |
michael@0 | 14 | SkNativeGLContext::AutoContextRestore::AutoContextRestore() { |
michael@0 | 15 | fOldGLXContext = glXGetCurrentContext(); |
michael@0 | 16 | fOldDisplay = glXGetCurrentDisplay(); |
michael@0 | 17 | fOldDrawable = glXGetCurrentDrawable(); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { |
michael@0 | 21 | if (NULL != fOldDisplay) { |
michael@0 | 22 | glXMakeCurrent(fOldDisplay, fOldDrawable, fOldGLXContext); |
michael@0 | 23 | } |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 27 | |
michael@0 | 28 | static bool ctxErrorOccurred = false; |
michael@0 | 29 | static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) { |
michael@0 | 30 | ctxErrorOccurred = true; |
michael@0 | 31 | return 0; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | SkNativeGLContext::SkNativeGLContext() |
michael@0 | 35 | : fContext(NULL) |
michael@0 | 36 | , fDisplay(NULL) |
michael@0 | 37 | , fPixmap(0) |
michael@0 | 38 | , fGlxPixmap(0) { |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | SkNativeGLContext::~SkNativeGLContext() { |
michael@0 | 42 | this->destroyGLContext(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | void SkNativeGLContext::destroyGLContext() { |
michael@0 | 46 | if (fDisplay) { |
michael@0 | 47 | glXMakeCurrent(fDisplay, 0, 0); |
michael@0 | 48 | |
michael@0 | 49 | if (fContext) { |
michael@0 | 50 | glXDestroyContext(fDisplay, fContext); |
michael@0 | 51 | fContext = NULL; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | if (fGlxPixmap) { |
michael@0 | 55 | glXDestroyGLXPixmap(fDisplay, fGlxPixmap); |
michael@0 | 56 | fGlxPixmap = 0; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | if (fPixmap) { |
michael@0 | 60 | XFreePixmap(fDisplay, fPixmap); |
michael@0 | 61 | fPixmap = 0; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | XCloseDisplay(fDisplay); |
michael@0 | 65 | fDisplay = NULL; |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | const GrGLInterface* SkNativeGLContext::createGLContext() { |
michael@0 | 70 | fDisplay = XOpenDisplay(0); |
michael@0 | 71 | |
michael@0 | 72 | if (!fDisplay) { |
michael@0 | 73 | SkDebugf("Failed to open X display.\n"); |
michael@0 | 74 | this->destroyGLContext(); |
michael@0 | 75 | return NULL; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // Get a matching FB config |
michael@0 | 79 | static int visual_attribs[] = { |
michael@0 | 80 | GLX_X_RENDERABLE , True, |
michael@0 | 81 | GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT, |
michael@0 | 82 | None |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | #ifdef GLX_1_3 |
michael@0 | 86 | //SkDebugf("Getting matching framebuffer configs.\n"); |
michael@0 | 87 | int fbcount; |
michael@0 | 88 | GLXFBConfig *fbc = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay), |
michael@0 | 89 | visual_attribs, &fbcount); |
michael@0 | 90 | if (!fbc) { |
michael@0 | 91 | SkDebugf("Failed to retrieve a framebuffer config.\n"); |
michael@0 | 92 | this->destroyGLContext(); |
michael@0 | 93 | return NULL; |
michael@0 | 94 | } |
michael@0 | 95 | //SkDebugf("Found %d matching FB configs.\n", fbcount); |
michael@0 | 96 | |
michael@0 | 97 | // Pick the FB config/visual with the most samples per pixel |
michael@0 | 98 | //SkDebugf("Getting XVisualInfos.\n"); |
michael@0 | 99 | int best_fbc = -1, best_num_samp = -1; |
michael@0 | 100 | |
michael@0 | 101 | int i; |
michael@0 | 102 | for (i = 0; i < fbcount; ++i) { |
michael@0 | 103 | XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, fbc[i]); |
michael@0 | 104 | if (vi) { |
michael@0 | 105 | int samp_buf, samples; |
michael@0 | 106 | glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf); |
michael@0 | 107 | glXGetFBConfigAttrib(fDisplay, fbc[i], GLX_SAMPLES, &samples); |
michael@0 | 108 | |
michael@0 | 109 | //SkDebugf(" Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d," |
michael@0 | 110 | // " SAMPLES = %d\n", |
michael@0 | 111 | // i, (unsigned int)vi->visualid, samp_buf, samples); |
michael@0 | 112 | |
michael@0 | 113 | if (best_fbc < 0 || (samp_buf && samples > best_num_samp)) |
michael@0 | 114 | best_fbc = i, best_num_samp = samples; |
michael@0 | 115 | } |
michael@0 | 116 | XFree(vi); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | GLXFBConfig bestFbc = fbc[best_fbc]; |
michael@0 | 120 | |
michael@0 | 121 | // Be sure to free the FBConfig list allocated by glXChooseFBConfig() |
michael@0 | 122 | XFree(fbc); |
michael@0 | 123 | |
michael@0 | 124 | // Get a visual |
michael@0 | 125 | XVisualInfo *vi = glXGetVisualFromFBConfig(fDisplay, bestFbc); |
michael@0 | 126 | //SkDebugf("Chosen visual ID = 0x%x\n", (unsigned int)vi->visualid); |
michael@0 | 127 | #else |
michael@0 | 128 | int numVisuals; |
michael@0 | 129 | XVisualInfo visTemplate, *visReturn; |
michael@0 | 130 | |
michael@0 | 131 | visReturn = XGetVisualInfo(fDisplay, VisualNoMask, &visTemplate, &numVisuals); |
michael@0 | 132 | if (NULL == visReturn) |
michael@0 | 133 | { |
michael@0 | 134 | SkDebugf("Failed to get visual information.\n"); |
michael@0 | 135 | this->destroyGLContext(); |
michael@0 | 136 | return NULL; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | int best = -1, best_num_samp = -1; |
michael@0 | 140 | |
michael@0 | 141 | for (int i = 0; i < numVisuals; ++i) |
michael@0 | 142 | { |
michael@0 | 143 | int samp_buf, samples; |
michael@0 | 144 | |
michael@0 | 145 | glXGetConfig(fDisplay, &visReturn[i], GLX_SAMPLE_BUFFERS, &samp_buf); |
michael@0 | 146 | glXGetConfig(fDisplay, &visReturn[i], GLX_SAMPLES, &samples); |
michael@0 | 147 | |
michael@0 | 148 | if (best < 0 || (samp_buf && samples > best_num_samp)) |
michael@0 | 149 | best = i, best_num_samp = samples; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | XVisualInfo temp = visReturn[best]; |
michael@0 | 153 | XVisualInfo *vi = &temp; |
michael@0 | 154 | |
michael@0 | 155 | XFree(visReturn); |
michael@0 | 156 | #endif |
michael@0 | 157 | |
michael@0 | 158 | fPixmap = XCreatePixmap(fDisplay, RootWindow(fDisplay, vi->screen), 10, 10, vi->depth); |
michael@0 | 159 | |
michael@0 | 160 | if (!fPixmap) { |
michael@0 | 161 | SkDebugf("Failed to create pixmap.\n"); |
michael@0 | 162 | this->destroyGLContext(); |
michael@0 | 163 | return NULL; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | fGlxPixmap = glXCreateGLXPixmap(fDisplay, vi, fPixmap); |
michael@0 | 167 | |
michael@0 | 168 | #ifdef GLX_1_3 |
michael@0 | 169 | // Done with the visual info data |
michael@0 | 170 | XFree(vi); |
michael@0 | 171 | #endif |
michael@0 | 172 | |
michael@0 | 173 | // Create the context |
michael@0 | 174 | |
michael@0 | 175 | // Install an X error handler so the application won't exit if GL 3.0 |
michael@0 | 176 | // context allocation fails. |
michael@0 | 177 | // |
michael@0 | 178 | // Note this error handler is global. |
michael@0 | 179 | // All display connections in all threads of a process use the same |
michael@0 | 180 | // error handler, so be sure to guard against other threads issuing |
michael@0 | 181 | // X commands while this code is running. |
michael@0 | 182 | ctxErrorOccurred = false; |
michael@0 | 183 | int (*oldHandler)(Display*, XErrorEvent*) = |
michael@0 | 184 | XSetErrorHandler(&ctxErrorHandler); |
michael@0 | 185 | |
michael@0 | 186 | // Get the default screen's GLX extension list |
michael@0 | 187 | const char *glxExts = glXQueryExtensionsString( |
michael@0 | 188 | fDisplay, DefaultScreen(fDisplay) |
michael@0 | 189 | ); |
michael@0 | 190 | // Check for the GLX_ARB_create_context extension string and the function. |
michael@0 | 191 | // If either is not present, use GLX 1.3 context creation method. |
michael@0 | 192 | if (!gluCheckExtension( |
michael@0 | 193 | reinterpret_cast<const GLubyte*>("GLX_ARB_create_context") |
michael@0 | 194 | , reinterpret_cast<const GLubyte*>(glxExts))) |
michael@0 | 195 | { |
michael@0 | 196 | //SkDebugf("GLX_ARB_create_context not found." |
michael@0 | 197 | // " Using old-style GLX context.\n"); |
michael@0 | 198 | #ifdef GLX_1_3 |
michael@0 | 199 | fContext = glXCreateNewContext(fDisplay, bestFbc, GLX_RGBA_TYPE, 0, True); |
michael@0 | 200 | #else |
michael@0 | 201 | fContext = glXCreateContext(fDisplay, vi, 0, True); |
michael@0 | 202 | #endif |
michael@0 | 203 | |
michael@0 | 204 | } |
michael@0 | 205 | #ifdef GLX_1_3 |
michael@0 | 206 | else { |
michael@0 | 207 | //SkDebugf("Creating context.\n"); |
michael@0 | 208 | |
michael@0 | 209 | PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = |
michael@0 | 210 | (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((GrGLubyte*)"glXCreateContextAttribsARB"); |
michael@0 | 211 | int context_attribs[] = { |
michael@0 | 212 | GLX_CONTEXT_MAJOR_VERSION_ARB, 3, |
michael@0 | 213 | GLX_CONTEXT_MINOR_VERSION_ARB, 0, |
michael@0 | 214 | //GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, |
michael@0 | 215 | None |
michael@0 | 216 | }; |
michael@0 | 217 | fContext = glXCreateContextAttribsARB( |
michael@0 | 218 | fDisplay, bestFbc, 0, True, context_attribs |
michael@0 | 219 | ); |
michael@0 | 220 | |
michael@0 | 221 | // Sync to ensure any errors generated are processed. |
michael@0 | 222 | XSync(fDisplay, False); |
michael@0 | 223 | if (!ctxErrorOccurred && fContext) { |
michael@0 | 224 | //SkDebugf( "Created GL 3.0 context.\n" ); |
michael@0 | 225 | } else { |
michael@0 | 226 | // Couldn't create GL 3.0 context. |
michael@0 | 227 | // Fall back to old-style 2.x context. |
michael@0 | 228 | // When a context version below 3.0 is requested, |
michael@0 | 229 | // implementations will return the newest context version compatible |
michael@0 | 230 | // with OpenGL versions less than version 3.0. |
michael@0 | 231 | |
michael@0 | 232 | // GLX_CONTEXT_MAJOR_VERSION_ARB = 1 |
michael@0 | 233 | context_attribs[1] = 1; |
michael@0 | 234 | // GLX_CONTEXT_MINOR_VERSION_ARB = 0 |
michael@0 | 235 | context_attribs[3] = 0; |
michael@0 | 236 | |
michael@0 | 237 | ctxErrorOccurred = false; |
michael@0 | 238 | |
michael@0 | 239 | //SkDebugf("Failed to create GL 3.0 context." |
michael@0 | 240 | // " Using old-style GLX context.\n"); |
michael@0 | 241 | fContext = glXCreateContextAttribsARB( |
michael@0 | 242 | fDisplay, bestFbc, 0, True, context_attribs |
michael@0 | 243 | ); |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | #endif |
michael@0 | 247 | |
michael@0 | 248 | // Sync to ensure any errors generated are processed. |
michael@0 | 249 | XSync(fDisplay, False); |
michael@0 | 250 | |
michael@0 | 251 | // Restore the original error handler |
michael@0 | 252 | XSetErrorHandler(oldHandler); |
michael@0 | 253 | |
michael@0 | 254 | if (ctxErrorOccurred || !fContext) { |
michael@0 | 255 | SkDebugf("Failed to create an OpenGL context.\n"); |
michael@0 | 256 | this->destroyGLContext(); |
michael@0 | 257 | return NULL; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | // Verify that context is a direct context |
michael@0 | 261 | if (!glXIsDirect(fDisplay, fContext)) { |
michael@0 | 262 | //SkDebugf("Indirect GLX rendering context obtained.\n"); |
michael@0 | 263 | } else { |
michael@0 | 264 | //SkDebugf("Direct GLX rendering context obtained.\n"); |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | //SkDebugf("Making context current.\n"); |
michael@0 | 268 | if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { |
michael@0 | 269 | SkDebugf("Could not set the context.\n"); |
michael@0 | 270 | this->destroyGLContext(); |
michael@0 | 271 | return NULL; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | const GrGLInterface* interface = GrGLCreateNativeInterface(); |
michael@0 | 275 | if (!interface) { |
michael@0 | 276 | SkDebugf("Failed to create gl interface"); |
michael@0 | 277 | this->destroyGLContext(); |
michael@0 | 278 | return NULL; |
michael@0 | 279 | } |
michael@0 | 280 | return interface; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | void SkNativeGLContext::makeCurrent() const { |
michael@0 | 284 | if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { |
michael@0 | 285 | SkDebugf("Could not set the context.\n"); |
michael@0 | 286 | } |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | void SkNativeGLContext::swapBuffers() const { |
michael@0 | 290 | glXSwapBuffers(fDisplay, fGlxPixmap); |
michael@0 | 291 | } |