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 | |
michael@0 | 9 | #include "SkWGL.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "SkTDArray.h" |
michael@0 | 12 | #include "SkTSearch.h" |
michael@0 | 13 | #include "SkTSort.h" |
michael@0 | 14 | |
michael@0 | 15 | bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const { |
michael@0 | 16 | if (NULL == this->fGetExtensionsString) { |
michael@0 | 17 | return false; |
michael@0 | 18 | } |
michael@0 | 19 | if (!strcmp("WGL_ARB_extensions_string", ext)) { |
michael@0 | 20 | return true; |
michael@0 | 21 | } |
michael@0 | 22 | const char* extensionString = this->getExtensionsString(dc); |
michael@0 | 23 | size_t extLength = strlen(ext); |
michael@0 | 24 | |
michael@0 | 25 | while (true) { |
michael@0 | 26 | size_t n = strcspn(extensionString, " "); |
michael@0 | 27 | if (n == extLength && 0 == strncmp(ext, extensionString, n)) { |
michael@0 | 28 | return true; |
michael@0 | 29 | } |
michael@0 | 30 | if (0 == extensionString[n]) { |
michael@0 | 31 | return false; |
michael@0 | 32 | } |
michael@0 | 33 | extensionString += n+1; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | return false; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | const char* SkWGLExtensions::getExtensionsString(HDC hdc) const { |
michael@0 | 40 | return fGetExtensionsString(hdc); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | BOOL SkWGLExtensions::choosePixelFormat(HDC hdc, |
michael@0 | 44 | const int* piAttribIList, |
michael@0 | 45 | const FLOAT* pfAttribFList, |
michael@0 | 46 | UINT nMaxFormats, |
michael@0 | 47 | int* piFormats, |
michael@0 | 48 | UINT* nNumFormats) const { |
michael@0 | 49 | return fChoosePixelFormat(hdc, piAttribIList, pfAttribFList, |
michael@0 | 50 | nMaxFormats, piFormats, nNumFormats); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | BOOL SkWGLExtensions::getPixelFormatAttribiv(HDC hdc, |
michael@0 | 54 | int iPixelFormat, |
michael@0 | 55 | int iLayerPlane, |
michael@0 | 56 | UINT nAttributes, |
michael@0 | 57 | const int *piAttributes, |
michael@0 | 58 | int *piValues) const { |
michael@0 | 59 | return fGetPixelFormatAttribiv(hdc, iPixelFormat, iLayerPlane, |
michael@0 | 60 | nAttributes, piAttributes, piValues); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | BOOL SkWGLExtensions::getPixelFormatAttribfv(HDC hdc, |
michael@0 | 64 | int iPixelFormat, |
michael@0 | 65 | int iLayerPlane, |
michael@0 | 66 | UINT nAttributes, |
michael@0 | 67 | const int *piAttributes, |
michael@0 | 68 | float *pfValues) const { |
michael@0 | 69 | return fGetPixelFormatAttribfv(hdc, iPixelFormat, iLayerPlane, |
michael@0 | 70 | nAttributes, piAttributes, pfValues); |
michael@0 | 71 | } |
michael@0 | 72 | HGLRC SkWGLExtensions::createContextAttribs(HDC hDC, |
michael@0 | 73 | HGLRC hShareContext, |
michael@0 | 74 | const int *attribList) const { |
michael@0 | 75 | return fCreateContextAttribs(hDC, hShareContext, attribList); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | namespace { |
michael@0 | 79 | |
michael@0 | 80 | struct PixelFormat { |
michael@0 | 81 | int fFormat; |
michael@0 | 82 | int fSampleCnt; |
michael@0 | 83 | int fChoosePixelFormatRank; |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | bool pf_less(const PixelFormat& a, const PixelFormat& b) { |
michael@0 | 87 | if (a.fSampleCnt < b.fSampleCnt) { |
michael@0 | 88 | return true; |
michael@0 | 89 | } else if (b.fSampleCnt < a.fSampleCnt) { |
michael@0 | 90 | return false; |
michael@0 | 91 | } else if (a.fChoosePixelFormatRank < b.fChoosePixelFormatRank) { |
michael@0 | 92 | return true; |
michael@0 | 93 | } |
michael@0 | 94 | return false; |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | int SkWGLExtensions::selectFormat(const int formats[], |
michael@0 | 99 | int formatCount, |
michael@0 | 100 | HDC dc, |
michael@0 | 101 | int desiredSampleCount) { |
michael@0 | 102 | PixelFormat desiredFormat = { |
michael@0 | 103 | 0, |
michael@0 | 104 | desiredSampleCount, |
michael@0 | 105 | 0, |
michael@0 | 106 | }; |
michael@0 | 107 | SkTDArray<PixelFormat> rankedFormats; |
michael@0 | 108 | rankedFormats.setCount(formatCount); |
michael@0 | 109 | for (int i = 0; i < formatCount; ++i) { |
michael@0 | 110 | static const int kQueryAttr = SK_WGL_SAMPLES; |
michael@0 | 111 | int numSamples; |
michael@0 | 112 | this->getPixelFormatAttribiv(dc, |
michael@0 | 113 | formats[i], |
michael@0 | 114 | 0, |
michael@0 | 115 | 1, |
michael@0 | 116 | &kQueryAttr, |
michael@0 | 117 | &numSamples); |
michael@0 | 118 | rankedFormats[i].fFormat = formats[i]; |
michael@0 | 119 | rankedFormats[i].fSampleCnt = numSamples; |
michael@0 | 120 | rankedFormats[i].fChoosePixelFormatRank = i; |
michael@0 | 121 | } |
michael@0 | 122 | SkTQSort(rankedFormats.begin(), |
michael@0 | 123 | rankedFormats.begin() + rankedFormats.count() - 1, |
michael@0 | 124 | SkTLessFunctionToFunctorAdaptor<PixelFormat, pf_less>()); |
michael@0 | 125 | int idx = SkTSearch<PixelFormat, pf_less>(rankedFormats.begin(), |
michael@0 | 126 | rankedFormats.count(), |
michael@0 | 127 | desiredFormat, |
michael@0 | 128 | sizeof(PixelFormat)); |
michael@0 | 129 | if (idx < 0) { |
michael@0 | 130 | idx = ~idx; |
michael@0 | 131 | } |
michael@0 | 132 | return rankedFormats[idx].fFormat; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | |
michael@0 | 136 | namespace { |
michael@0 | 137 | |
michael@0 | 138 | #if defined(UNICODE) |
michael@0 | 139 | #define STR_LIT(X) L## #X |
michael@0 | 140 | #else |
michael@0 | 141 | #define STR_LIT(X) #X |
michael@0 | 142 | #endif |
michael@0 | 143 | |
michael@0 | 144 | #define DUMMY_CLASS STR_LIT("DummyClass") |
michael@0 | 145 | |
michael@0 | 146 | HWND create_dummy_window() { |
michael@0 | 147 | HMODULE module = GetModuleHandle(NULL); |
michael@0 | 148 | HWND dummy; |
michael@0 | 149 | RECT windowRect; |
michael@0 | 150 | windowRect.left = 0; |
michael@0 | 151 | windowRect.right = 8; |
michael@0 | 152 | windowRect.top = 0; |
michael@0 | 153 | windowRect.bottom = 8; |
michael@0 | 154 | |
michael@0 | 155 | WNDCLASS wc; |
michael@0 | 156 | |
michael@0 | 157 | wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
michael@0 | 158 | wc.lpfnWndProc = (WNDPROC) DefWindowProc; |
michael@0 | 159 | wc.cbClsExtra = 0; |
michael@0 | 160 | wc.cbWndExtra = 0; |
michael@0 | 161 | wc.hInstance = module; |
michael@0 | 162 | wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); |
michael@0 | 163 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
michael@0 | 164 | wc.hbrBackground = NULL; |
michael@0 | 165 | wc.lpszMenuName = NULL; |
michael@0 | 166 | wc.lpszClassName = DUMMY_CLASS; |
michael@0 | 167 | |
michael@0 | 168 | if(!RegisterClass(&wc)) { |
michael@0 | 169 | return 0; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | DWORD style, exStyle; |
michael@0 | 173 | exStyle = WS_EX_CLIENTEDGE; |
michael@0 | 174 | style = WS_SYSMENU; |
michael@0 | 175 | |
michael@0 | 176 | AdjustWindowRectEx(&windowRect, style, false, exStyle); |
michael@0 | 177 | if(!(dummy = CreateWindowEx(exStyle, |
michael@0 | 178 | DUMMY_CLASS, |
michael@0 | 179 | STR_LIT("DummyWindow"), |
michael@0 | 180 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | style, |
michael@0 | 181 | 0, 0, |
michael@0 | 182 | windowRect.right-windowRect.left, |
michael@0 | 183 | windowRect.bottom-windowRect.top, |
michael@0 | 184 | NULL, NULL, |
michael@0 | 185 | module, |
michael@0 | 186 | NULL))) { |
michael@0 | 187 | UnregisterClass(DUMMY_CLASS, module); |
michael@0 | 188 | return NULL; |
michael@0 | 189 | } |
michael@0 | 190 | ShowWindow(dummy, SW_HIDE); |
michael@0 | 191 | |
michael@0 | 192 | return dummy; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | void destroy_dummy_window(HWND dummy) { |
michael@0 | 196 | DestroyWindow(dummy); |
michael@0 | 197 | HMODULE module = GetModuleHandle(NULL); |
michael@0 | 198 | UnregisterClass(DUMMY_CLASS, module); |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | #define GET_PROC(NAME, SUFFIX) f##NAME = \ |
michael@0 | 203 | (##NAME##Proc) wglGetProcAddress("wgl" #NAME #SUFFIX) |
michael@0 | 204 | |
michael@0 | 205 | SkWGLExtensions::SkWGLExtensions() |
michael@0 | 206 | : fGetExtensionsString(NULL) |
michael@0 | 207 | , fChoosePixelFormat(NULL) |
michael@0 | 208 | , fGetPixelFormatAttribfv(NULL) |
michael@0 | 209 | , fGetPixelFormatAttribiv(NULL) |
michael@0 | 210 | , fCreateContextAttribs(NULL) { |
michael@0 | 211 | HDC prevDC = wglGetCurrentDC(); |
michael@0 | 212 | HGLRC prevGLRC = wglGetCurrentContext(); |
michael@0 | 213 | |
michael@0 | 214 | PIXELFORMATDESCRIPTOR dummyPFD; |
michael@0 | 215 | |
michael@0 | 216 | ZeroMemory(&dummyPFD, sizeof(dummyPFD)); |
michael@0 | 217 | dummyPFD.nSize = sizeof(dummyPFD); |
michael@0 | 218 | dummyPFD.nVersion = 1; |
michael@0 | 219 | dummyPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; |
michael@0 | 220 | dummyPFD.iPixelType = PFD_TYPE_RGBA; |
michael@0 | 221 | dummyPFD.cColorBits = 32; |
michael@0 | 222 | dummyPFD.cDepthBits = 0; |
michael@0 | 223 | dummyPFD.cStencilBits = 8; |
michael@0 | 224 | dummyPFD.iLayerType = PFD_MAIN_PLANE; |
michael@0 | 225 | HWND dummyWND = create_dummy_window(); |
michael@0 | 226 | if (dummyWND) { |
michael@0 | 227 | HDC dummyDC = GetDC(dummyWND); |
michael@0 | 228 | int dummyFormat = ChoosePixelFormat(dummyDC, &dummyPFD); |
michael@0 | 229 | SetPixelFormat(dummyDC, dummyFormat, &dummyPFD); |
michael@0 | 230 | HGLRC dummyGLRC = wglCreateContext(dummyDC); |
michael@0 | 231 | SkASSERT(dummyGLRC); |
michael@0 | 232 | wglMakeCurrent(dummyDC, dummyGLRC); |
michael@0 | 233 | |
michael@0 | 234 | GET_PROC(GetExtensionsString, ARB); |
michael@0 | 235 | GET_PROC(ChoosePixelFormat, ARB); |
michael@0 | 236 | GET_PROC(GetPixelFormatAttribiv, ARB); |
michael@0 | 237 | GET_PROC(GetPixelFormatAttribfv, ARB); |
michael@0 | 238 | GET_PROC(CreateContextAttribs, ARB); |
michael@0 | 239 | |
michael@0 | 240 | wglMakeCurrent(dummyDC, NULL); |
michael@0 | 241 | wglDeleteContext(dummyGLRC); |
michael@0 | 242 | destroy_dummy_window(dummyWND); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | wglMakeCurrent(prevDC, prevGLRC); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool preferCoreProfile) { |
michael@0 | 249 | SkWGLExtensions extensions; |
michael@0 | 250 | if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) { |
michael@0 | 251 | return NULL; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | HDC prevDC = wglGetCurrentDC(); |
michael@0 | 255 | HGLRC prevGLRC = wglGetCurrentContext(); |
michael@0 | 256 | PIXELFORMATDESCRIPTOR pfd; |
michael@0 | 257 | |
michael@0 | 258 | int format = 0; |
michael@0 | 259 | |
michael@0 | 260 | static const int iAttrs[] = { |
michael@0 | 261 | SK_WGL_DRAW_TO_WINDOW, TRUE, |
michael@0 | 262 | SK_WGL_DOUBLE_BUFFER, TRUE, |
michael@0 | 263 | SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION, |
michael@0 | 264 | SK_WGL_SUPPORT_OPENGL, TRUE, |
michael@0 | 265 | SK_WGL_COLOR_BITS, 24, |
michael@0 | 266 | SK_WGL_ALPHA_BITS, 8, |
michael@0 | 267 | SK_WGL_STENCIL_BITS, 8, |
michael@0 | 268 | 0, 0 |
michael@0 | 269 | }; |
michael@0 | 270 | |
michael@0 | 271 | float fAttrs[] = {0, 0}; |
michael@0 | 272 | |
michael@0 | 273 | if (msaaSampleCount > 0 && |
michael@0 | 274 | extensions.hasExtension(dc, "WGL_ARB_multisample")) { |
michael@0 | 275 | static const int kIAttrsCount = SK_ARRAY_COUNT(iAttrs); |
michael@0 | 276 | int msaaIAttrs[kIAttrsCount + 4]; |
michael@0 | 277 | memcpy(msaaIAttrs, iAttrs, sizeof(int) * kIAttrsCount); |
michael@0 | 278 | SkASSERT(0 == msaaIAttrs[kIAttrsCount - 2] && |
michael@0 | 279 | 0 == msaaIAttrs[kIAttrsCount - 1]); |
michael@0 | 280 | msaaIAttrs[kIAttrsCount - 2] = SK_WGL_SAMPLE_BUFFERS; |
michael@0 | 281 | msaaIAttrs[kIAttrsCount - 1] = TRUE; |
michael@0 | 282 | msaaIAttrs[kIAttrsCount + 0] = SK_WGL_SAMPLES; |
michael@0 | 283 | msaaIAttrs[kIAttrsCount + 1] = msaaSampleCount; |
michael@0 | 284 | msaaIAttrs[kIAttrsCount + 2] = 0; |
michael@0 | 285 | msaaIAttrs[kIAttrsCount + 3] = 0; |
michael@0 | 286 | unsigned int num; |
michael@0 | 287 | int formats[64]; |
michael@0 | 288 | extensions.choosePixelFormat(dc, msaaIAttrs, fAttrs, 64, formats, &num); |
michael@0 | 289 | num = SkTMin(num, 64U); |
michael@0 | 290 | int formatToTry = extensions.selectFormat(formats, |
michael@0 | 291 | num, |
michael@0 | 292 | dc, |
michael@0 | 293 | msaaSampleCount); |
michael@0 | 294 | DescribePixelFormat(dc, formatToTry, sizeof(pfd), &pfd); |
michael@0 | 295 | if (SetPixelFormat(dc, formatToTry, &pfd)) { |
michael@0 | 296 | format = formatToTry; |
michael@0 | 297 | } |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | if (0 == format) { |
michael@0 | 301 | // Either MSAA wasn't requested or creation failed |
michael@0 | 302 | unsigned int num; |
michael@0 | 303 | extensions.choosePixelFormat(dc, iAttrs, fAttrs, 1, &format, &num); |
michael@0 | 304 | DescribePixelFormat(dc, format, sizeof(pfd), &pfd); |
michael@0 | 305 | SkDEBUGCODE(BOOL set =) SetPixelFormat(dc, format, &pfd); |
michael@0 | 306 | SkASSERT(TRUE == set); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | HGLRC glrc = NULL; |
michael@0 | 310 | if (preferCoreProfile && extensions.hasExtension(dc, "WGL_ARB_create_context")) { |
michael@0 | 311 | static const int kCoreGLVersions[] = { |
michael@0 | 312 | 4, 3, |
michael@0 | 313 | 4, 2, |
michael@0 | 314 | 4, 1, |
michael@0 | 315 | 4, 0, |
michael@0 | 316 | 3, 3, |
michael@0 | 317 | 3, 2, |
michael@0 | 318 | }; |
michael@0 | 319 | int coreProfileAttribs[] = { |
michael@0 | 320 | SK_WGL_CONTEXT_MAJOR_VERSION, -1, |
michael@0 | 321 | SK_WGL_CONTEXT_MINOR_VERSION, -1, |
michael@0 | 322 | SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT, |
michael@0 | 323 | 0, |
michael@0 | 324 | }; |
michael@0 | 325 | for (int v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) { |
michael@0 | 326 | coreProfileAttribs[1] = kCoreGLVersions[2 * v]; |
michael@0 | 327 | coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1]; |
michael@0 | 328 | glrc = extensions.createContextAttribs(dc, NULL, coreProfileAttribs); |
michael@0 | 329 | if (NULL != glrc) { |
michael@0 | 330 | break; |
michael@0 | 331 | } |
michael@0 | 332 | } |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | if (NULL == glrc) { |
michael@0 | 336 | glrc = wglCreateContext(dc); |
michael@0 | 337 | } |
michael@0 | 338 | SkASSERT(glrc); |
michael@0 | 339 | |
michael@0 | 340 | wglMakeCurrent(prevDC, prevGLRC); |
michael@0 | 341 | return glrc; |
michael@0 | 342 | } |