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