bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame^] | 9 | #include "SkTypes.h" |
| 10 | #if defined(SK_BUILD_FOR_WIN32) |
| 11 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 12 | #include "SkWGL.h" |
| 13 | |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
| 15 | #include "SkTSearch.h" |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 16 | #include "SkTSort.h" |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 18 | bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | if (nullptr == this->fGetExtensionsString) { |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 20 | return false; |
| 21 | } |
| 22 | if (!strcmp("WGL_ARB_extensions_string", ext)) { |
| 23 | return true; |
| 24 | } |
| 25 | const char* extensionString = this->getExtensionsString(dc); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 26 | size_t extLength = strlen(ext); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 27 | |
| 28 | while (true) { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 29 | size_t n = strcspn(extensionString, " "); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 30 | if (n == extLength && 0 == strncmp(ext, extensionString, n)) { |
| 31 | return true; |
| 32 | } |
| 33 | if (0 == extensionString[n]) { |
| 34 | return false; |
| 35 | } |
| 36 | extensionString += n+1; |
| 37 | } |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | const char* SkWGLExtensions::getExtensionsString(HDC hdc) const { |
| 43 | return fGetExtensionsString(hdc); |
| 44 | } |
| 45 | |
| 46 | BOOL SkWGLExtensions::choosePixelFormat(HDC hdc, |
| 47 | const int* piAttribIList, |
| 48 | const FLOAT* pfAttribFList, |
| 49 | UINT nMaxFormats, |
| 50 | int* piFormats, |
| 51 | UINT* nNumFormats) const { |
| 52 | return fChoosePixelFormat(hdc, piAttribIList, pfAttribFList, |
| 53 | nMaxFormats, piFormats, nNumFormats); |
| 54 | } |
| 55 | |
| 56 | BOOL SkWGLExtensions::getPixelFormatAttribiv(HDC hdc, |
| 57 | int iPixelFormat, |
| 58 | int iLayerPlane, |
| 59 | UINT nAttributes, |
| 60 | const int *piAttributes, |
| 61 | int *piValues) const { |
| 62 | return fGetPixelFormatAttribiv(hdc, iPixelFormat, iLayerPlane, |
| 63 | nAttributes, piAttributes, piValues); |
| 64 | } |
| 65 | |
| 66 | BOOL SkWGLExtensions::getPixelFormatAttribfv(HDC hdc, |
| 67 | int iPixelFormat, |
| 68 | int iLayerPlane, |
| 69 | UINT nAttributes, |
| 70 | const int *piAttributes, |
| 71 | float *pfValues) const { |
| 72 | return fGetPixelFormatAttribfv(hdc, iPixelFormat, iLayerPlane, |
| 73 | nAttributes, piAttributes, pfValues); |
| 74 | } |
| 75 | HGLRC SkWGLExtensions::createContextAttribs(HDC hDC, |
| 76 | HGLRC hShareContext, |
| 77 | const int *attribList) const { |
| 78 | return fCreateContextAttribs(hDC, hShareContext, attribList); |
| 79 | } |
| 80 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 81 | BOOL SkWGLExtensions::swapInterval(int interval) const { |
| 82 | return fSwapInterval(interval); |
| 83 | } |
| 84 | |
| 85 | HPBUFFER SkWGLExtensions::createPbuffer(HDC hDC, |
| 86 | int iPixelFormat, |
| 87 | int iWidth, |
| 88 | int iHeight, |
| 89 | const int *piAttribList) const { |
| 90 | return fCreatePbuffer(hDC, iPixelFormat, iWidth, iHeight, piAttribList); |
| 91 | } |
| 92 | |
| 93 | HDC SkWGLExtensions::getPbufferDC(HPBUFFER hPbuffer) const { |
| 94 | return fGetPbufferDC(hPbuffer); |
| 95 | } |
| 96 | |
| 97 | int SkWGLExtensions::releasePbufferDC(HPBUFFER hPbuffer, HDC hDC) const { |
| 98 | return fReleasePbufferDC(hPbuffer, hDC); |
| 99 | } |
| 100 | |
| 101 | BOOL SkWGLExtensions::destroyPbuffer(HPBUFFER hPbuffer) const { |
| 102 | return fDestroyPbuffer(hPbuffer); |
| 103 | } |
| 104 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 105 | namespace { |
| 106 | |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 107 | struct PixelFormat { |
| 108 | int fFormat; |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 109 | int fSampleCnt; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 110 | int fChoosePixelFormatRank; |
| 111 | }; |
| 112 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 113 | bool pf_less(const PixelFormat& a, const PixelFormat& b) { |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 114 | if (a.fSampleCnt < b.fSampleCnt) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 115 | return true; |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 116 | } else if (b.fSampleCnt < a.fSampleCnt) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } else if (a.fChoosePixelFormatRank < b.fChoosePixelFormatRank) { |
| 119 | return true; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 120 | } |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 121 | return false; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | int SkWGLExtensions::selectFormat(const int formats[], |
| 126 | int formatCount, |
| 127 | HDC dc, |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 128 | int desiredSampleCount) const { |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 129 | PixelFormat desiredFormat = { |
| 130 | 0, |
| 131 | desiredSampleCount, |
| 132 | 0, |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 133 | }; |
| 134 | SkTDArray<PixelFormat> rankedFormats; |
| 135 | rankedFormats.setCount(formatCount); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 136 | for (int i = 0; i < formatCount; ++i) { |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 137 | static const int kQueryAttr = SK_WGL_SAMPLES; |
| 138 | int numSamples; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 139 | this->getPixelFormatAttribiv(dc, |
| 140 | formats[i], |
| 141 | 0, |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 142 | 1, |
| 143 | &kQueryAttr, |
| 144 | &numSamples); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 145 | rankedFormats[i].fFormat = formats[i]; |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 146 | rankedFormats[i].fSampleCnt = numSamples; |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 147 | rankedFormats[i].fChoosePixelFormatRank = i; |
| 148 | } |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 149 | SkTQSort(rankedFormats.begin(), |
| 150 | rankedFormats.begin() + rankedFormats.count() - 1, |
| 151 | SkTLessFunctionToFunctorAdaptor<PixelFormat, pf_less>()); |
| 152 | int idx = SkTSearch<PixelFormat, pf_less>(rankedFormats.begin(), |
| 153 | rankedFormats.count(), |
| 154 | desiredFormat, |
| 155 | sizeof(PixelFormat)); |
bsalomon@google.com | 8a189b0 | 2012-04-17 12:43:00 +0000 | [diff] [blame] | 156 | if (idx < 0) { |
| 157 | idx = ~idx; |
| 158 | } |
| 159 | return rankedFormats[idx].fFormat; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | namespace { |
| 164 | |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 165 | #if defined(UNICODE) |
| 166 | #define STR_LIT(X) L## #X |
| 167 | #else |
| 168 | #define STR_LIT(X) #X |
| 169 | #endif |
| 170 | |
| 171 | #define DUMMY_CLASS STR_LIT("DummyClass") |
| 172 | |
| 173 | HWND create_dummy_window() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | HMODULE module = GetModuleHandle(nullptr); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 175 | HWND dummy; |
| 176 | RECT windowRect; |
| 177 | windowRect.left = 0; |
| 178 | windowRect.right = 8; |
| 179 | windowRect.top = 0; |
| 180 | windowRect.bottom = 8; |
| 181 | |
| 182 | WNDCLASS wc; |
| 183 | |
| 184 | wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
| 185 | wc.lpfnWndProc = (WNDPROC) DefWindowProc; |
| 186 | wc.cbClsExtra = 0; |
| 187 | wc.cbWndExtra = 0; |
| 188 | wc.hInstance = module; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 189 | wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO); |
| 190 | wc.hCursor = LoadCursor(nullptr, IDC_ARROW); |
| 191 | wc.hbrBackground = nullptr; |
| 192 | wc.lpszMenuName = nullptr; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 193 | wc.lpszClassName = DUMMY_CLASS; |
| 194 | |
| 195 | if(!RegisterClass(&wc)) { |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | DWORD style, exStyle; |
| 200 | exStyle = WS_EX_CLIENTEDGE; |
| 201 | style = WS_SYSMENU; |
| 202 | |
| 203 | AdjustWindowRectEx(&windowRect, style, false, exStyle); |
| 204 | if(!(dummy = CreateWindowEx(exStyle, |
| 205 | DUMMY_CLASS, |
| 206 | STR_LIT("DummyWindow"), |
| 207 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | style, |
| 208 | 0, 0, |
| 209 | windowRect.right-windowRect.left, |
| 210 | windowRect.bottom-windowRect.top, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 211 | nullptr, nullptr, |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 212 | module, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 213 | nullptr))) { |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 214 | UnregisterClass(DUMMY_CLASS, module); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | return nullptr; |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 216 | } |
| 217 | ShowWindow(dummy, SW_HIDE); |
| 218 | |
| 219 | return dummy; |
| 220 | } |
| 221 | |
| 222 | void destroy_dummy_window(HWND dummy) { |
| 223 | DestroyWindow(dummy); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 224 | HMODULE module = GetModuleHandle(nullptr); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 225 | UnregisterClass(DUMMY_CLASS, module); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | #define GET_PROC(NAME, SUFFIX) f##NAME = \ |
| 230 | (##NAME##Proc) wglGetProcAddress("wgl" #NAME #SUFFIX) |
| 231 | |
| 232 | SkWGLExtensions::SkWGLExtensions() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 233 | : fGetExtensionsString(nullptr) |
| 234 | , fChoosePixelFormat(nullptr) |
| 235 | , fGetPixelFormatAttribfv(nullptr) |
| 236 | , fGetPixelFormatAttribiv(nullptr) |
| 237 | , fCreateContextAttribs(nullptr) |
| 238 | , fSwapInterval(nullptr) |
| 239 | , fCreatePbuffer(nullptr) |
| 240 | , fGetPbufferDC(nullptr) |
| 241 | , fReleasePbufferDC(nullptr) |
| 242 | , fDestroyPbuffer(nullptr) |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 243 | { |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 244 | HDC prevDC = wglGetCurrentDC(); |
| 245 | HGLRC prevGLRC = wglGetCurrentContext(); |
| 246 | |
| 247 | PIXELFORMATDESCRIPTOR dummyPFD; |
| 248 | |
| 249 | ZeroMemory(&dummyPFD, sizeof(dummyPFD)); |
| 250 | dummyPFD.nSize = sizeof(dummyPFD); |
| 251 | dummyPFD.nVersion = 1; |
| 252 | dummyPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; |
| 253 | dummyPFD.iPixelType = PFD_TYPE_RGBA; |
| 254 | dummyPFD.cColorBits = 32; |
| 255 | dummyPFD.cDepthBits = 0; |
| 256 | dummyPFD.cStencilBits = 8; |
| 257 | dummyPFD.iLayerType = PFD_MAIN_PLANE; |
| 258 | HWND dummyWND = create_dummy_window(); |
| 259 | if (dummyWND) { |
| 260 | HDC dummyDC = GetDC(dummyWND); |
| 261 | int dummyFormat = ChoosePixelFormat(dummyDC, &dummyPFD); |
| 262 | SetPixelFormat(dummyDC, dummyFormat, &dummyPFD); |
| 263 | HGLRC dummyGLRC = wglCreateContext(dummyDC); |
| 264 | SkASSERT(dummyGLRC); |
| 265 | wglMakeCurrent(dummyDC, dummyGLRC); |
| 266 | |
| 267 | GET_PROC(GetExtensionsString, ARB); |
| 268 | GET_PROC(ChoosePixelFormat, ARB); |
| 269 | GET_PROC(GetPixelFormatAttribiv, ARB); |
| 270 | GET_PROC(GetPixelFormatAttribfv, ARB); |
| 271 | GET_PROC(CreateContextAttribs, ARB); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 272 | GET_PROC(SwapInterval, EXT); |
| 273 | GET_PROC(CreatePbuffer, ARB); |
| 274 | GET_PROC(GetPbufferDC, ARB); |
| 275 | GET_PROC(ReleasePbufferDC, ARB); |
| 276 | GET_PROC(DestroyPbuffer, ARB); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 277 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 278 | wglMakeCurrent(dummyDC, nullptr); |
bsalomon@google.com | bd7c641 | 2011-12-01 16:34:28 +0000 | [diff] [blame] | 279 | wglDeleteContext(dummyGLRC); |
| 280 | destroy_dummy_window(dummyWND); |
| 281 | } |
| 282 | |
| 283 | wglMakeCurrent(prevDC, prevGLRC); |
| 284 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 285 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 286 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 287 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 288 | static void get_pixel_formats_to_try(HDC dc, const SkWGLExtensions& extensions, |
| 289 | bool doubleBuffered, int msaaSampleCount, |
| 290 | int formatsToTry[2]) { |
| 291 | int iAttrs[] = { |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 292 | SK_WGL_DRAW_TO_WINDOW, TRUE, |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 293 | SK_WGL_DOUBLE_BUFFER, (doubleBuffered ? TRUE : FALSE), |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 294 | SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION, |
| 295 | SK_WGL_SUPPORT_OPENGL, TRUE, |
| 296 | SK_WGL_COLOR_BITS, 24, |
| 297 | SK_WGL_ALPHA_BITS, 8, |
| 298 | SK_WGL_STENCIL_BITS, 8, |
| 299 | 0, 0 |
| 300 | }; |
| 301 | |
| 302 | float fAttrs[] = {0, 0}; |
| 303 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 304 | // Get a MSAA format if requested and possible. |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 305 | if (msaaSampleCount > 0 && |
| 306 | extensions.hasExtension(dc, "WGL_ARB_multisample")) { |
| 307 | static const int kIAttrsCount = SK_ARRAY_COUNT(iAttrs); |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 308 | int msaaIAttrs[kIAttrsCount + 4]; |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 309 | memcpy(msaaIAttrs, iAttrs, sizeof(int) * kIAttrsCount); |
| 310 | SkASSERT(0 == msaaIAttrs[kIAttrsCount - 2] && |
| 311 | 0 == msaaIAttrs[kIAttrsCount - 1]); |
| 312 | msaaIAttrs[kIAttrsCount - 2] = SK_WGL_SAMPLE_BUFFERS; |
| 313 | msaaIAttrs[kIAttrsCount - 1] = TRUE; |
| 314 | msaaIAttrs[kIAttrsCount + 0] = SK_WGL_SAMPLES; |
| 315 | msaaIAttrs[kIAttrsCount + 1] = msaaSampleCount; |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 316 | msaaIAttrs[kIAttrsCount + 2] = 0; |
| 317 | msaaIAttrs[kIAttrsCount + 3] = 0; |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 318 | unsigned int num; |
| 319 | int formats[64]; |
| 320 | extensions.choosePixelFormat(dc, msaaIAttrs, fAttrs, 64, formats, &num); |
bsalomon@google.com | f3f2d16 | 2013-07-31 18:52:31 +0000 | [diff] [blame] | 321 | num = SkTMin(num, 64U); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 322 | formatsToTry[0] = extensions.selectFormat(formats, num, dc, msaaSampleCount); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 325 | // Get a non-MSAA format |
| 326 | int* format = -1 == formatsToTry[0] ? &formatsToTry[0] : &formatsToTry[1]; |
| 327 | unsigned int num; |
| 328 | extensions.choosePixelFormat(dc, iAttrs, fAttrs, 1, format, &num); |
| 329 | } |
| 330 | |
| 331 | static HGLRC create_gl_context(HDC dc, SkWGLExtensions extensions, SkWGLContextRequest contextType) { |
| 332 | HDC prevDC = wglGetCurrentDC(); |
| 333 | HGLRC prevGLRC = wglGetCurrentContext(); |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 334 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 335 | HGLRC glrc = nullptr; |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 336 | if (kGLES_SkWGLContextRequest == contextType) { |
| 337 | if (!extensions.hasExtension(dc, "WGL_EXT_create_context_es2_profile")) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 338 | wglMakeCurrent(prevDC, prevGLRC); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 339 | return nullptr; |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 340 | } |
| 341 | static const int glesAttribs[] = { |
| 342 | SK_WGL_CONTEXT_MAJOR_VERSION, 3, |
| 343 | SK_WGL_CONTEXT_MINOR_VERSION, 0, |
| 344 | SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_ES2_PROFILE_BIT, |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 345 | 0, |
| 346 | }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 347 | glrc = extensions.createContextAttribs(dc, nullptr, glesAttribs); |
| 348 | if (nullptr == glrc) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 349 | wglMakeCurrent(prevDC, prevGLRC); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 350 | return nullptr; |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 351 | } |
| 352 | } else { |
| 353 | if (kGLPreferCoreProfile_SkWGLContextRequest == contextType && |
| 354 | extensions.hasExtension(dc, "WGL_ARB_create_context")) { |
| 355 | static const int kCoreGLVersions[] = { |
| 356 | 4, 3, |
| 357 | 4, 2, |
| 358 | 4, 1, |
| 359 | 4, 0, |
| 360 | 3, 3, |
| 361 | 3, 2, |
| 362 | }; |
| 363 | int coreProfileAttribs[] = { |
| 364 | SK_WGL_CONTEXT_MAJOR_VERSION, -1, |
| 365 | SK_WGL_CONTEXT_MINOR_VERSION, -1, |
| 366 | SK_WGL_CONTEXT_PROFILE_MASK, SK_WGL_CONTEXT_CORE_PROFILE_BIT, |
| 367 | 0, |
| 368 | }; |
| 369 | for (int v = 0; v < SK_ARRAY_COUNT(kCoreGLVersions) / 2; ++v) { |
| 370 | coreProfileAttribs[1] = kCoreGLVersions[2 * v]; |
| 371 | coreProfileAttribs[3] = kCoreGLVersions[2 * v + 1]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 372 | glrc = extensions.createContextAttribs(dc, nullptr, coreProfileAttribs); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 373 | if (glrc) { |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 374 | break; |
| 375 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 380 | if (nullptr == glrc) { |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 381 | glrc = wglCreateContext(dc); |
| 382 | } |
| 383 | SkASSERT(glrc); |
| 384 | |
| 385 | wglMakeCurrent(prevDC, prevGLRC); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 386 | |
| 387 | // This might help make the context non-vsynced. |
| 388 | if (extensions.hasExtension(dc, "WGL_EXT_swap_control")) { |
| 389 | extensions.swapInterval(-1); |
| 390 | } |
bsalomon@google.com | b7f20f2 | 2013-03-05 19:13:09 +0000 | [diff] [blame] | 391 | return glrc; |
| 392 | } |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 393 | |
| 394 | HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, SkWGLContextRequest contextType) { |
| 395 | SkWGLExtensions extensions; |
| 396 | if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 397 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | BOOL set = FALSE; |
| 401 | |
| 402 | int pixelFormatsToTry[] = { -1, -1 }; |
| 403 | get_pixel_formats_to_try(dc, extensions, true, msaaSampleCount, pixelFormatsToTry); |
| 404 | for (int f = 0; |
| 405 | !set && -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFormatsToTry); |
| 406 | ++f) { |
| 407 | PIXELFORMATDESCRIPTOR pfd; |
| 408 | DescribePixelFormat(dc, pixelFormatsToTry[f], sizeof(pfd), &pfd); |
| 409 | set = SetPixelFormat(dc, pixelFormatsToTry[f], &pfd); |
| 410 | } |
| 411 | |
| 412 | if (!set) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 413 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return create_gl_context(dc, extensions, contextType);} |
| 417 | |
| 418 | SkWGLPbufferContext* SkWGLPbufferContext::Create(HDC parentDC, int msaaSampleCount, |
| 419 | SkWGLContextRequest contextType) { |
| 420 | SkWGLExtensions extensions; |
| 421 | if (!extensions.hasExtension(parentDC, "WGL_ARB_pixel_format") || |
| 422 | !extensions.hasExtension(parentDC, "WGL_ARB_pbuffer")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 423 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // try for single buffer first |
| 427 | for (int dblBuffer = 0; dblBuffer < 2; ++dblBuffer) { |
| 428 | int pixelFormatsToTry[] = { -1, -1 }; |
| 429 | get_pixel_formats_to_try(parentDC, extensions, (0 != dblBuffer), msaaSampleCount, |
| 430 | pixelFormatsToTry); |
| 431 | for (int f = 0; -1 != pixelFormatsToTry[f] && f < SK_ARRAY_COUNT(pixelFormatsToTry); ++f) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 432 | HPBUFFER pbuf = extensions.createPbuffer(parentDC, pixelFormatsToTry[f], 1, 1, nullptr); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 433 | if (0 != pbuf) { |
| 434 | HDC dc = extensions.getPbufferDC(pbuf); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 435 | if (dc) { |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 436 | HGLRC glrc = create_gl_context(dc, extensions, contextType); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 437 | if (glrc) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 438 | return new SkWGLPbufferContext(pbuf, dc, glrc); |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 439 | } |
| 440 | extensions.releasePbufferDC(pbuf, dc); |
| 441 | } |
| 442 | extensions.destroyPbuffer(pbuf); |
| 443 | } |
| 444 | } |
| 445 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 446 | return nullptr; |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | SkWGLPbufferContext::~SkWGLPbufferContext() { |
| 450 | SkASSERT(fExtensions.hasExtension(fDC, "WGL_ARB_pbuffer")); |
| 451 | wglDeleteContext(fGLRC); |
| 452 | fExtensions.releasePbufferDC(fPbuffer, fDC); |
| 453 | fExtensions.destroyPbuffer(fPbuffer); |
| 454 | } |
| 455 | |
| 456 | SkWGLPbufferContext::SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc) |
| 457 | : fPbuffer(pbuffer) |
| 458 | , fDC(dc) |
| 459 | , fGLRC(glrc) { |
| 460 | } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame^] | 461 | |
| 462 | #endif//defined(SK_BUILD_FOR_WIN32) |